Entwickler-Ecke

Sonstiges (Delphi) - Status von Word97 feststellen


feivel3333 - Mi 18.05.05 16:55
Titel: Status von Word97 feststellen
Hallo,

mit WinWord := CreateOleObject( 'Word.Application' ) habe ich eine Word-Applikation erstellt.
Diese lässt sich von außen wunderbar steuern, bringt aber manchmal das Problem mit sich, dass ich schneller Befehle sende, als das Word sie verarbeiten kann.

Kennt von Euch jemand einen Befehl, um abzufragen, ob das Word seine Arbeit erledigt hat?
So habe ich beispielsweise ein Makro geschrieben, das im Word-Dokument eine von mir definierte Variable gegen Text austauscht. Allerdings musste ich mit einer For-Schleife eine künstliche Bremse einbauen:


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:
function Word_ReplaceText( oWord97 : Variant; csVariable, csText : string ) : boolean;

var niI : integer;


begin
   // Zuerst Cursor an den Anfang der ersten Zelle der Tabelle bringen
   result := true;
   try
      Application.ProcessMessages;
      oWord97.Selection.HomeKey( Unit := wdLine );
      oWord97.Selection.MoveUp( Unit := wdLine, Count := 8 );
      oWord97.Selection.HomeKey( Unit := wdLine );
      oWord97.Selection.MoveLeft( Unit := wdCharacter, Count := 1 );
      oWord97.Selection.HomeKey( Unit := wdLine );
      oWord97.Selection.MoveLeft( Unit := wdCharacter, Count := 1 );
      oWord97.Selection.HomeKey( Unit := wdLine );
      oWord97.Selection.MoveLeft( Unit := wdCharacter, Count := 1 );
      oWord97.Selection.HomeKey( Unit := wdLine );


      // Künstliche Bremse:
      for niI := 1 to 60000 do begin
         Application.ProcessMessages;
      end;

      // Variable austauschen:
      oWord97.Selection.Find.ClearFormatting;
      oWord97.Selection.Find.Replacement.ClearFormatting;
      oWord97.Selection.Find.Text := csVariable;
      oWord97.Selection.Find.Replacement.Text := csText;
      oWord97.Selection.Find.Forward := True;
      oWord97.Selection.Find.Wrap := wdFindContinue;
      oWord97.Selection.Find.Format := False;
      oWord97.Selection.Find.MatchCase := False;
      oWord97.Selection.Find.MatchWholeWord := False;
      oWord97.Selection.Find.MatchWildcards := False;
      oWord97.Selection.Find.MatchSoundsLike := False;
      oWord97.Selection.Find.MatchAllWordForms := False;
      oWord97.Selection.Find.Execute;

      if oWord97.Selection.Find.Forward = True then
          oWord97.Selection.Collapse( Direction := wdCollapseStart )
      else
          oWord97.Selection.Collapse( Direction := wdCollapseEnd );

      oWord97.Selection.Find.Execute( Replace := wdReplaceOne );

      If oWord97.Selection.Find.Forward = True then
          oWord97.Selection.Collapse( Direction := wdCollapseEnd )
      else
          oWord97.Selection.Collapse( Direction := wdCollapseStart );

      oWord97.Selection.Find.Execute;
   except
      result := false;
   end;

   // Künstliche Bremse:
   for niI := 1 to 60000 do begin
      Application.ProcessMessages;
   end;
end;


Diese Bremse, die bis 60000 zählt, bremst natürlich auch die Erstellungsgeschwindigkeit des Word-Dokuments. Und ich muss ja nicht auf Word warten, wenn es nicht unbedingt notwendig ist.

Viel besser wäre es doch sicher, wenn man anstatt dieser elendig lang zählenden Schleife einen Wartebefehl hätte, der so lange wartet, bis Word mit seinem ihm aufgetragenen Job fertig ist.

Wäre ja alles nicht so schlimm, wenn diese Routine nicht einige tausend Male am Tag ausgeführt würde.

Für jeden Tip bin ich sehr dankbar.

Gruß, Feivel


EDIT:

Habe es selbst herausgefunden, nachdem ich eine Laufzeitanalyse durchgeführt habe:
Nach jedem Befehl, der an Word gesendet wird, muss ein Application.ProcessMessages ausgeführt werden, ansonsten gibt es einen Message-Stau, und das System hängt sich auf.