Autor Beitrag
Redrunner
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 14.09.06 07:52 
Hallo

Bin gerade soweit:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleServer, WordXP, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WordApplication1: TWordApplication;
    Button2: TButton;
    WordDocument1: TWordDocument;
    OpenDialog1: TOpenDialog;
    Button3: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit3: TEdit;
    Label3: TLabel;
    Edit4: TEdit;
    Label4: TLabel;
    Edit5: TEdit;
    Label5: TLabel;
    Edit6: TEdit;
    Label6: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    WordApplication2: TWordApplication;
    WordDocument2: TWordDocument;
    OpenDialog2: TOpenDialog;
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
WordApplication1.Connect;
WordApplication1.Visible:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
WordApplication1.Quit;
end;

procedure TForm1.Button3Click(Sender: TObject);
var FileName:OleVariant;
vWhat, vBookmark:OleVariant;
begin
if checkbox1.Checked then
FileName := 'C:\Dell\KonfigFilial.doc';
WordApplication1.Connect;
WordApplication1.Documents.OpenOld(FileName, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
WordApplication1.Visible := True;
vWhat:=wdGoToBookmark;

vBookmark:='Hostname';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit1.text);

vBookmark:='Neighbour';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit2.text);

vBookmark:='Neighbour2';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit3.text);

vBookmark:='RouterIP';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit4.text);

vBookmark:='Standort';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit5.text);

vBookmark:='SwitchIP';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication1.Selection.TypeText (Edit6.text);

///////////////////////////////////////////////////////////////////////////////

if checkbox2.Checked then
FileName := 'C:\Dell\KonfigKasse.doc';
WordApplication2.Connect;
WordApplication2.Documents.OpenOld(FileName, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
WordDocument2.ConnectTo(WordApplication2.ActiveDocument);
WordApplication2.Visible := True;
vWhat:=wdGoToBookmark;

vBookmark:='Hostname';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit1.text);

vBookmark:='Neighbour';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit2.text);

vBookmark:='Neighbour2';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit3.text);

vBookmark:='RouterIP';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit4.text);

vBookmark:='Standort';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit5.text);

vBookmark:='SwitchIP';
WordApplication2.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
WordApplication2.Selection.TypeText (Edit6.text);


End;

end.


Bekomme jetzt aber immer einen Fehler, wenn ich die 2. Checkbox nach der 1. anklicke und auf den Button klicke. Irgendwas dass der RPC Server nicht vorhanden ist ... ???

Weiß einer was da falsch ist ?
Danke
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 14.09.06 07:57 
zu jedem connect gehört ein disconnect und die fehlen Dir.

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Redrunner Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 14.09.06 09:02 
Zitat:
zu jedem connect gehört ein disconnect und die fehlen Dir.


Ah ok, hab das jetzt mal probiert mit WordApplication1.disconnect, ich weiß aber nicht genau wo ich das in meinem Quelltext setzen muss, damit es funktioniert ??
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 14.09.06 14:36 
wichtig ist, das du für jedes connect ein disconnect hast. Wenn der anwender z.B. Word geschlossen hat, reicht ein erneutes connect nicht aus um dich erneut mit Word zu verbinden.

Du hast für mich 2 Möglichkeiten:
1. 1x connect und disconnect am Anfang/Ende deines Programmes, also z.B. Formcreate und formdestroy. Problem hierbei: der Anwender darf Word dazwichen nicht mal einfach so beenden. Ein weiterer Versuch, per Ole etwas auszuführen wird dann fehlschlagen. Du kannst zwar mit den Ereignissen der Wordapp und Worddoc auf das schließen reagieren und dich ggf. neu diconnecten und neu verbinden, da gibt es aber einen kleinen Bug, wenn du erst das doc und dann die app schließt, bekommst du ein ereignis nicht ausgelöst, irgendwas war da, weiß aber im moment nicht mehr genau was. Aber im Moment habe ich nich soviel zeit, daß rauszusuchen.
2. Möglichkeit: connect und disconnect direkt bei der Bearbeitung, für dich wahrscheinlich besser.
also z.B.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
WordApplication1.Connect;  
WordApplication1.Documents.OpenOld(FileName, EmptyParam, EmptyParam,  
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,  
EmptyParam, EmptyParam);  
WordDocument1.ConnectTo(WordApplication1.ActiveDocument);  
WordApplication1.Visible := True;  
vWhat:=wdGoToBookmark;  
 
vBookmark:='Hostname';  
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);  
WordApplication1.Selection.TypeText (Edit1.text);  
...


WordDocument1.disconnect;
WordApplication1.disConnect;

Ich würde dann außerdem noch eine try-finally drum bauen, damit sichergestellt ist, das das diconnect immer aufgerufen wird. wenn deine Gotos ins leere laufen, weil die Textmarke mal nicht exisitiert, siehst du ziemlich alt aus.

Noch ein paar Hinweise:

  • warum 2 Wordapps und nicht 2 Worddocs?
  • die Gotos und typetext würde ich in eine Proc auslagern und nur den Namen der Bookmark und Text übergeben, liest sich besser und du hast etliche Zeilen weniger.
  • Du öffnest Doc´s. du hast keine Chance zu verhindern, daß der Anwender das Dokument "aus versehen" speichert. Überleg Dir, ob du das nicht doch besser über eine Vorlage löst, bei der ein Überschreiben des ursprünglichen Doks nicht möglich ist.
  • Wenn ich mich recht entsinne bist du mit dem Goto direkt auf der Textmarke und dein Typetext überschreibt Dir die Textmarke. Wenn einer das Doc gespeichert hat, sind deine Textmarken futsch. Verwende evtl insertafter oder verwende statt einer Textmarke gleich ein Textformularfeld, das du füllst. Textformularfelder erkennst du besser in deinem Doc als eine Textmarke.


Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)