Solved

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

Posted on 2011-03-15
9
187 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 19

Expert Comment

by:akoster
ID: 35139496
sure, give me a moment...
0
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

820 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