Autor Beitrag
havanna
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 13.06.02 09:43 
Hallo,
hat zufällig jemand Erfahrung mit Word-Fernsteuerung? Ich wollte aus Delphi(5) mit der upgedateten Server Komponente ein Word-Dokument öffnen und Text suchen und ersetzen. Dazu habe ich folgendes Coding:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
    FileName:=Result+'\office\order.doc';
    WordApplication1.Connect;
    WordApplication1.Documents.OpenOld(FileName, EmptyParam, EmptyParam,
                                       EmptyParam, EmptyParam, EmptyParam,
                                       EmptyParam, EmptyParam, EmptyParam,
                                       EmptyParam);
    Doc.ConnectTo(WordApplication1.ActiveDocument);
    WordApplication1.Visible:= true;
    { start to replace }
    SearchText:= '
order_id';
    ReplaceText:= Module.Query27.FieldByName('order_id').AsString;
    Wrap := wdFindContinue;
    All := wdReplaceAll;
    Doc.Content.Find.Execute(SearchText, EmptyParam, EmptyParam, EmptyParam,
                             EmptyParam, EmptyParam, EmptyParam, Wrap,
                             EmptyParam, ReplaceText, All, EmptyParam,
                             EmptyParam, EmptyParam, EmptyParam);
Bei Doc.Content.Find.Execute... bekomme ich eine EOleException: Das Stub erhielt falsche Daten.

Hat jemand eine Idee, woran das liegen könnte?

Im Voraus besten Dank.
havanna

Code-Tags hinzugefügt. Tino
Urba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 47



BeitragVerfasst: Mo 17.06.02 22:13 
Servus,

Ich habe da eine Seite, die dir vielleicht weiterhilft

www.djpate.freeserve.co.uk/Automation.htm

_________________
Es gibt keine blöden Fragen, nur blöde Antworten :)
havanna Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 20.06.02 17:14 
Titel: Danke für den Tipp
den Link hatte ich auch schon gefunden. Allerdings hat er mir nicht viel weitergeholfen. :( Wäre vielleicht ja auch einmal ein Thema für ein Tutorial. Trotzdem danke. Vielleicht weiß ja noch jemand anderes etwas zu meinem Coding zu sagen.

havanna
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Do 20.06.02 17:20 
Versuch es doch mal so:
ausblenden Quelltext
1:
2:
3:
Doc.Content.Find.Execute(SearchText, EmptyParam, EmptyParam, EmptyParam,
               EmptyParam, EmptyParam, EmptyParam, Wrap, 
               EmptyParam, ReplaceText, All);

Also die letzten 4 Parameter entfernen!

Gruß
Marc
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 169

Win 2000, Win XP, SuSE Linux 8.2

BeitragVerfasst: Do 20.06.02 22:33 
Hi havanna,

welche Version von Office hast Du denn? Mit den Delphi-Komponenten kann Office 97 ferngesteuert werden. Office 2000 war ja bei Erscheinen von Delphi 5 noch nicht auf dem Markt. Installiere Dir das ServicePack 1 für Delphi, dann kannst Du auch die COM-Objekte benutzen um Office 2000 fernzusteuern.

Gruß Marc
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Fr 21.06.02 00:00 
Hi,

Ich hab auch mal damit rumprobiert aber irgendwie funktioniert's
nicht so richtig. Es kommt bei
WordDocument.Content.Find.Execute() immer die Fehlermeldung
"Das Stub enthielt falsche Daten".
IMO sind aber die Parameter korrekt.

OS: XP
Office: XP
Delphi: 6 Prof


ausblenden volle Höhe 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:
function Word_SearchReplace(ADocument: TFileName; SearchString, ReplaceString: string): Boolean;
var
  WordApplication: TWordApplication;
  WordDocument: TWordDocument;
  FileName, _Find, _Replace, _All, _Wrap: OleVariant;
begin
  { Assume Failure }
  Result := False;

  FileName := ADocument;
  { Connect to Word }
  WordApplication := TWordApplication.Create(nil);
  try
    WordApplication.Connect;
    WordApplication.Documents.Open(FileName, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, EmptyParam);

    WordDocument := TWordDocument.Create(nil);
    { Connect to Active Document }
    WordDocument.ConnectTo(WordApplication.ActiveDocument);
    try
      { Initialize parameters}
      _Wrap := wdFindContinue;
      _All := wdReplaceAll;
      _Find := SearchString;
      _Replace := ReplaceString;

      { Perform the search}
      WordDocument.Content.Find.Execute(_Find, EmptyParam, EmptyParam,
        EmptyParam, EmptyParam, EmptyParam,
        EmptyParam, _Wrap, EmptyParam, _Replace, EmptyParam, EmptyParam,
        EmptyParam, EmptyParam, _All);

      { Close Word }
    finally
      WordDocument.Close;
      WordDocument.Disconnect;
    end;
    // WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
    { Save }
    WordDocument.SaveAs(FileName);
    WordDocument.Disconnect;
    WordDocument.Free;
    Result:= True;
  finally
    { Quit }
    WordApplication.Quit;
    WordApplication.Disconnect;
    WordApplication.Free;
  end;
end;
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Fr 21.06.02 15:17 
Hier ein Tutorial zum Thema das ich in AUQ.de! eingefügt habe!

Gruß
havanna Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 25.06.02 15:38 
Titel: Word - Replace
Hallo zusammen,
Das Tutorial ist ganz gut aber es hilft eben überhaupt nicht bei diesem Problem (am Thema vorbei).
Erst einmal vielen Dank für die Tipps. Nachdem es etwas länger ging mit den Reaktionen hatte ich zwischenzeitlich im Entwicklerforum das Thema aufgemacht und dort auch sehr gute Hilfe erhalten. Anscheinend liegt es an der frühen Bindung. Mit später Bindung funktioniert es.
Hier der Link für den Fall das sich jemand dafür interessiert.

http://www.entwickler-forum.de/webx?50@202.1SkGaC3biQe^15642@.ee8ccf1/3

havanna
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Fr 05.07.02 10:17 
Hi,

Ich hab mir nun eine solche Funktion gecoded:

ausblenden volle Höhe 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:
uses
  ComObj;

type
  TWordReplaceFlags = set of (wrfReplaceAll, wrfMatchCase, wrfMatchWildcards);

function Word_StringReplace(ADocument: TFileName; SearchString, ReplaceString: string; Flags: TWordReplaceFlags): Boolean;
const
  wdFindContinue = 1;
  wdReplaceOne = 1;
  wdReplaceAll = 2;
  wdDoNotSaveChanges = 0;
var
  WordApp: OLEVariant;
begin
  Result := False;

  { Check if file exists }
  if not FileExists(ADocument) then
  begin
    ShowMessage('Specified Document not found.');
    Exit;
  end;

  { Create the OLE Object }
  try
    WordApp := CreateOLEObject('Word.Application');
  except
    on E: Exception do
    begin
      E.Message := 'Word is not available.';
      raise;
    end;
  end;

  try
    { Hide Word }
    WordApp.Visible := False;
    { Open the document }
    WordApp.Documents.Open(ADocument);
    { Initialize parameters}
    WordApp.Selection.Find.ClearFormatting;
    WordApp.Selection.Find.Text := SearchString;
    WordApp.Selection.Find.Replacement.Text := ReplaceString;
    WordApp.Selection.Find.Forward := True;
    WordApp.Selection.Find.Wrap := wdFindContinue;
    WordApp.Selection.Find.Format := False;
    WordApp.Selection.Find.MatchCase := wrfMatchCase in Flags;
    WordApp.Selection.Find.MatchWholeWord := False;
    WordApp.Selection.Find.MatchWildcards := wrfMatchWildcards in Flags;
    WordApp.Selection.Find.MatchSoundsLike := False;
    WordApp.Selection.Find.MatchAllWordForms := False;
    { Perform the search}
    if wrfReplaceAll in Flags then
      WordApp.Selection.Find.Execute(Replace := wdReplaceAll)
    else
      WordApp.Selection.Find.Execute(Replace := wdReplaceOne);
    { Save word }
    WordApp.ActiveDocument.SaveAs(ADocument);
    { Assume that successful }
    Result := True;
    { Close the document }
    WordApp.ActiveDocument.Close(wdDoNotSaveChanges);
  finally
    { Quit Word }
    WordApp.Quit;
    WordApp := Unassigned;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin                                                 
  Word_StringReplace('C:\Test.doc','Old String','New String',[wrfReplaceAll]);
end;