I have a Word Document that contains MERGEFIELDs which I would like to fill from a Delphi application. My first thought was: use OLE Automation. This seems to be the right idea but I have difficulty implementing this.
What have I done so far? Well, I created a Word document and inserted a MERGEFIELD via menu Insert / Field... / MergeField. I gave this field the name "title".
Here is the code:
procedure TForm1.Button2Click(Sender: TObject);
var
FileName, name, VarName, VarValue: OleVariant;
i, cnt: Integer;
doc: TWordDocument;
begin
FileName := 'I:\tmp\test.doc';
wordApp.Connect;
wordApp.Documents.OpenOld( FileName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
wordApp.Visible := true;
// How do I use: wordApp.ActiveDocument.MailMerge. ?
// How do I create a DataSource?
end;
I understood that I need a DataSource that contains the actual data I would like the word document to contain after my program ran.
I created a text file mydata.txt that looks like:
title
MyTitle1
MyTitle2
How do I tell wordApp.ActiveDocument that it should use mydata.txt as a data source?
What have I done so far? Well, I created a Word document and inserted a MERGEFIELD via menu Insert / Field... / MergeField. I gave this field the name "title".
Here is the code:
procedure TForm1.Button2Click(Sender: TObject);
var
FileName, name, VarName, VarValue: OleVariant;
i, cnt: Integer;
doc: TWordDocument;
begin
FileName := 'I:\tmp\test.doc';
wordApp.Connect;
wordApp.Documents.OpenOld( FileName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
wordApp.Visible := true;
// How do I use: wordApp.ActiveDocument.MailMerge. ?
// How do I create a DataSource?
end;
I understood that I need a DataSource that contains the actual data I would like the word document to contain after my program ran.
I created a text file mydata.txt that looks like:
title
MyTitle1
MyTitle2
How do I tell wordApp.ActiveDocument that it should use mydata.txt as a data source?