Solved

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

Posted on 2011-03-15
9
184 Views
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,
JP
0
Comment
Question by:easycapital
  • 4
  • 3
9 Comments
 
LVL 3

Expert Comment

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

Expert Comment

by:akoster
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]")
0
 

Author Comment

by:easycapital
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?

Thanks,
JP excel-mail-merge-example.xls word-mail-merge.doc
0
 
LVL 19

Expert Comment

by:akoster
ID: 35139496
sure, give me a moment...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 19

Accepted Solution

by:
akoster earned 500 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
    doc.Close
    word.Quit
    Set doc = Nothing
    Set word = Nothing

End Sub

Open in new window

0
 

Author Comment

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

Expert Comment

by:akoster
ID: 35718594
Teylyn,

the solution posed in comment

35139604

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

Author Closing Comment

by:easycapital
ID: 35747434
Thank you.

Sorry for the delay.  I have been swamped.

JP
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now