Originally Posted by
marcov
In eerste instantie zou ik hetzelfde zeggen als Rvk, breek de lange gechainde statements op en assign het aan olevariant.
Zoals ik in mijn laatste post (in de PS en extra PS) al aangaf, werkte dat ook niet.
Even ter volledigheid:
Het probleem is dat FPC in 3.2.0 (Lazarus 2.0.10) benoemde parameters (named parameters) niet meer accepteerde in OLE calls. In FPC 3.0.4 (Lazarus 2.0.8) werkte dit nog wel. Zie ook bugreport https://bugs.freepascal.org/view.php?id=37355. Die staat als fixed dus het zal in trunk wel weer werken (wat ik niet getest heb).
Dus dit werkte niet (geeft een foutmelding)
Delphi Code:
WordApp.Selection.Find.Execute(Replace := 1);
maar dit gaf geen foutmelding
Delphi Code:
WordApp.Selection.Find.Execute;
Echter bleek dit ook niet goed te werken (er werd niets vervangen).
De uiteindelijke oplossing is om alle parameters gewoon 'unnamed' mee te geven.
Uiteindelijke werkende code (gebruikmakend van ActiveDocument.Content i.p.v. Selection):
Delphi Code:
uses Variants, ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
WordApp : OleVariant;
w : widestring;
S1, S2 : OLEVariant;
const
wdFindContinue = 1;
wdReplaceOne = 1;
wdReplaceAll = 2;
begin
try
WordApp := CreateOLEObject('Word.Application');
except
WriteLn('Unable to start Word.');
Exit;
end;
S1 := 'XXXX';
S2 := 'YYYY';
w:= UTF8Decode('C:\temp\test.docx');
WordApp.Visible := True;
WordApp.Documents.Open(w);
// FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format,
// ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl
WordApp.ActiveDocument.Content.Find.Execute('<lastname>', false, false, false, false, false, true, false, false, S1, 1);
WordApp.ActiveDocument.Content.Find.Execute('<firstname>', false, false, false, false, false, true, false, false, S2, 1);
w:= UTF8Decode('C:\temp\test2.docx');
WordApp.ActiveDocument.SaveAs(w);
WordApp.ActiveDocument.Close;
WordApp.Quit;
WordApp := Unassigned;
end;
Bookmarks