Solved

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

Posted on 2011-03-15
9
185 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

948 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

20 Experts available now in Live!

Get 1:1 Help Now