Macro to fill out MS word fields from excel fields (2003 version)

1) Copy Field A1, and B1 from current excel sheet called "Summary"
2) Open a specific template MS word or a document located on a specific computer location
3) Paste info from Step 1 into specific sections of the Word document.

Extremely valuable.
Thank you,
JP
easycapitalAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Arno KosterConnect With a Mentor Commented:
Paste this macro in the sheet1 part in the Excel VBA editor
Update the actual path to the desktop location of the files

Sub perform_mail_merge()

    '-- open word application
    Set word = CreateObject("Word.Application")
    word.Visible = True
    
    '-- open document
    word.documents.Open "C:\[path to desktop]\word-mail-merge.doc"
    Set doc = word.activedocument
    
    '-- process fields
    doc.Content.Text = Replace(doc.Content.Text, "AAA", Range("A1"))
    doc.Content.Text = Replace(doc.Content.Text, "XXX", Range("B1"))
    
    '-- save document
    doc.SaveAs "C:\[path to desktop]\merged_document.doc"
    
    '-- close word application
    doc.Close
    word.Quit
    Set doc = Nothing
    Set word = Nothing

End Sub

Open in new window

0
 
AndrewK80Commented:
You can do this using the mail merge function.
0
 
Arno KosterCommented:

As AndrewK80 indicated, this is best performed using standard mail merge function.
When you want to perform it by code, it comes down to something like

Word.ActiveDocument.Content.Text = Replace(Word.ActiveDocument.Content.Text, "[word field]", "[excel field]")
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
easycapitalAuthor Commented:
I am addressing the initial question by attaching an excel example with values in A1 and B1. Then I indicate in the word document where A1 and B1 should go by typing AAA and XXX respectively.

Assuming both documents are saved in the desktop, could you provide the solution including the macro the runs it?

Thanks,
JP excel-mail-merge-example.xls word-mail-merge.doc
0
 
Arno KosterCommented:
sure, give me a moment...
0
 
easycapitalAuthor Commented:
I need a bit more time.
0
 
Arno KosterCommented:
Teylyn,

the solution posed in comment

35139604

is, in my opinion, a correct answer to the question of the author.
0
 
easycapitalAuthor Commented:
Thank you.

Sorry for the delay.  I have been swamped.

JP
0
All Courses

From novice to tech pro — start learning today.