Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TWordApplication, OLE Automation

Status
Not open for further replies.

johleimer

Programmer
Aug 30, 2004
1
DE
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?

 
instead of actually performing a mail merge, I use the following code to insert the information directly into the field in the Word document. However, I don't use the TWordDocument, I use the late binding (?) method like this:

Code:
var
wordapp, doc : variant;

wordapp := CreateOleObject('Word.Application');

doc := Word.Documents.Add('R:\Case Management\JMSv2\Forms\PaymentLetter.dot');

WordDoc.FormFields.Item('bmLastName').Result := qryName.FieldByName('LASTNAME').asString

Leslie
 
To attach a datasource to your document use the OpenDataSource method, the first parameter will be the name of the datasource file, e.g.

SourceFileName := 'C:\MyData.txt';
WordApp.ActiveDocument.MailMerge.OpenDataSource(SourceFileName, ...

And to perform the mail merge use the Execute method, e.g

MyPause := True;
WordApp.ActiveDocument.MailMerge.Execute(MyPause);

where SourceFileName and MyPause are OLEVariants. The best way to find the properties and methods of Word documents is to look in the help file of the visual basic editor in Word (under the Tools|Macro menu)

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top