Improve company productivity with a Business Account.Sign Up


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

Posted on 2011-03-15
Medium Priority
Last Modified: 2012-05-11
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,
Question by:easycapital
  • 4
  • 3

Expert Comment

ID: 35138759
You can do this using the mail merge function.
LVL 19

Expert Comment

by:Arno Koster
ID: 35139351

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]")

Author Comment

ID: 35139456
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?

JP excel-mail-merge-example.xls word-mail-merge.doc
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 19

Expert Comment

by:Arno Koster
ID: 35139496
sure, give me a moment...
LVL 19

Accepted Solution

Arno Koster earned 2000 total points
ID: 35139604
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
    Set doc = Nothing
    Set word = Nothing

End Sub

Open in new window


Author Comment

ID: 35244504
I need a bit more time.
LVL 19

Expert Comment

by:Arno Koster
ID: 35718594

the solution posed in comment


is, in my opinion, a correct answer to the question of the author.

Author Closing Comment

ID: 35747434
Thank you.

Sorry for the delay.  I have been swamped.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Usually, rounding is performed by some power of 10 - to thousands, hundreds, tens, or integer - or to one, two, or more decimals. But rounding can also be done to a power of two, say, 16 or 64, or 1/32 or 1/1024, even for extreme values.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question