Article ID: 138283
Article Last Modified on 10/11/2006
138289 XL7: Error Using OLE Automation with Microsoft Word 7.0
A1: NAME B1: ADDRESS C1: CITY D1: STATE E1: ZIP_CODE A2: John Smith B2: 1 Way Avenue C2: Kent D2: NC E2: 11111 A3: Mary Lynn B3: Do Drive C3: Poluk D3: SC E3: 22222 A4: Peter Paul B4: Fest Drive C4: Low D4: FL E4: 33333
Sub Do_It_All_Form_Letter()
Dim word As Object
Set word = CreateObject("word.basic") 'creates the word object
With word
.AppShow ' Makes Word Visible
'Sends keystrokes to the Open Worksheet dialog box
SendKeys "{TAB}{TAB}{TAB}{ENTER}"
'Opens the Microsoft Excel file
.FileOpen Name:="c:\testdata.xls"
'Saves the file as a Microsoft Word document
.FileSaveAs Name:="c:\testdata.doc", Format:=0
.FileClose 'Closes the file
.FileNewDefault ' Opens up blank Word document
'Makes the active window a main document
.MailMergeMainDocumentType 0
'Open the data source
.MailMergeOpenDataSource Name:="c:\testdata.doc", _
LinkToSource:=0
'Activates the mail merge main document
.MailMergeEditMainDocument
'The following Insert commands place text into the Word
'document. You could change these commands to place any text in
'the document. This example uses a typical business letter
'format.
.Insert "3454 Blindside St." 'Inserts a string
.InsertPara 'Inserts a carriage return
.Insert "Columbia, GA 23287"
.InsertPara
.InsertPara
'the mergefields are the same as the column headings in the Excel
'workbook
.InsertMergeField MergeField:="NAME"
.InsertPara
.InsertMergeField MergeField:="ADDRESS"
.InsertPara
.InsertMergeField MergeField:="CITY"
.Insert ", "
.InsertMergeField MergeField:="STATE"
.Insert " "
.InsertMergeField MergeField:="ZIP_CODE"
.InsertPara
.InsertPara
.InsertPara
.Insert "Dear "
.InsertMergeField MergeField:="NAME"
.Insert ","
.InsertPara
.InsertPara
.Insert "Thank You For Your Support."
.InsertPara
.InsertPara
.InsertPara
.Insert "Sincerely,"
.InsertPara
.InsertPara
.InsertPara
.Insert "John M. Doe"
.MailMergeToDoc 'Merges data records with the main document
' Saves the active document with the specified name
.FileSaveAs Name:="c:\letters.doc"
End With
Set word = Nothing 'clears the object variable
End Sub
OLE
Additional query words: mail merge XL7
Keywords: kbprogramming KB138283