Autor Beitrag
zongo-joe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 134

win xp prof
D3, D4, D7
BeitragVerfasst: Fr 29.01.10 21:50 
Nabend,
ich habe da ein Problem:
ich möchte mit Delphi in Word Strings ersetzen, und zwar alle Texte zwischen * und # durch Text1, Text2, usw...
Die Länge der Texte zwischen * und # ist aber variabel.
Beispiel:

Das ist dein Beispieltext *PH1#, der ein *ggsfg# Beispiel zeigt
-> Das ist dein Beispieltext Text1, der ein Text2 Beispiel zeigt

Bisher habeich aber nur Code für Bearbeiten_Ersetzen gefunden:

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:
function Word_StringReplace(ADocument: TFileName; SearchString, ReplaceString: string; all:boolean): Boolean;
// benötig comObj
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 := false;
    WordApp.Selection.Find.MatchWholeWord := False;
    WordApp.Selection.Find.MatchWildcards := false;
    WordApp.Selection.Find.MatchSoundsLike := False;
    WordApp.Selection.Find.MatchAllWordForms := False;
    { Perform the search}
    WordApp.Selection.Find.Execute(Replace := emptyparam);
    if All 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.inWORDersetzenneu1Click(Sender: TObject);
begin
if opendialog3.execute then  begin
  Word_StringReplace(opendialog3.filename,'Old String','New String',true);
  ShellExecute(Handle, 'open', PChar(opendialog3.filename), 0, 0, SW_SHOWNORMAL);
end;
end;


Den Rest muss man wohl irgendwie mit

ausblenden Delphi-Quelltext
1:
2:
WordApplication1.Selection.Extend(ext);
WordApplication1.Selection.typetext('NEUER TEXT');

hindengeln, aber ich kriegs nicht hin.

Als Word Makro geht das ja nicht.

Könnt Ihr mir da helfen ?
Danke schon mal.
zongo-joe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 134

win xp prof
D3, D4, D7
BeitragVerfasst: So 31.01.10 17:49 
Hat sich erledigt, ich machs gezz mit nem Workaround.
wens interessiert :
ich lade das Dok in ein Richedit, bearbeite das dann da und lege eine Liste mit Strings und dem Text, der sie ersetzt an.
Das kann man dann mit Serch/Replace im Originaldokument anwenden.