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:
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; const wdFindContinue = 1; wdReplaceOne = 1; wdReplaceAll = 2; wdDoNotSaveChanges = 0; var WordApp: OLEVariant; begin Result := False;
if not FileExists(ADocument) then begin ShowMessage('Specified Document not found.'); Exit; end;
try WordApp := CreateOLEObject('Word.Application'); except on E: Exception do begin E.Message := 'Word is not available.'; raise; end; end;
try WordApp.Visible := false; WordApp.Documents.Open(ADocument); 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; WordApp.Selection.Find.Execute(Replace := emptyparam); if All then WordApp.Selection.Find.Execute(Replace := wdReplaceAll) else WordApp.Selection.Find.Execute(Replace := wdReplaceOne); WordApp.ActiveDocument.SaveAs(ADocument); Result := True; WordApp.ActiveDocument.Close(wdDoNotSaveChanges); finally 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
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.