Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default excel to word template code

Hello,
I need to create a word doc using a word template with merge fields in it.
I would like to have a button in excel that launches the word template and
populates it with data that is stored in various sheets of my excel workbook.
Is this fairly straight forward and does anyone have sample code for
populating a word template from excel?
Thank you,
Mark
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default excel to word template code

Tick M/soft Word Objects in Tools, References while your project is selected
in Excel VB Editor. It's best to use bookmarks rather than merge fields (you
insert them into the Word template by positioning your cursor and then
selecting Insert, Bookmark). The following code will do the trick:

Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bmk As Word.Bookmark
Set doc = objWord.Documents.Open("full path name of template")
For Each bmk In doc.Bookmarks
If bmk.Name = "First_Name" Then bmk.Range.Text = Range("A1").Value
If bmk.Name = "Address" Then bmk.Range.Text = Range("B1").Value
Next
'doc.Bookmarks("First_Name").Range.Text = Range("A1").Value
objWord.Visible = True

I like to use the loop rather than the remmed-out line in case the template
gets altered along the line and loses one of the bookmarks (in which case the
loop doesn't fall over).


"Markw911" wrote:

Hello,
I need to create a word doc using a word template with merge fields in it.
I would like to have a button in excel that launches the word template and
populates it with data that is stored in various sheets of my excel workbook.
Is this fairly straight forward and does anyone have sample code for
populating a word template from excel?
Thank you,
Mark

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default excel to word template code

Hi,
Thanks for the great reply. The code works well with one issue. I cannot
get the word doc to open until I run the routine a second time. The second
time I run it I get two word windows with no documents in them and then a
third window is open with the doc I want. The fields do get merged.
Any ideas on how to show the document?
Thanks,
Mark

"Smallweed" wrote:

Tick M/soft Word Objects in Tools, References while your project is selected
in Excel VB Editor. It's best to use bookmarks rather than merge fields (you
insert them into the Word template by positioning your cursor and then
selecting Insert, Bookmark). The following code will do the trick:

Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bmk As Word.Bookmark
Set doc = objWord.Documents.Open("full path name of template")
For Each bmk In doc.Bookmarks
If bmk.Name = "First_Name" Then bmk.Range.Text = Range("A1").Value
If bmk.Name = "Address" Then bmk.Range.Text = Range("B1").Value
Next
'doc.Bookmarks("First_Name").Range.Text = Range("A1").Value
objWord.Visible = True

I like to use the loop rather than the remmed-out line in case the template
gets altered along the line and loses one of the bookmarks (in which case the
loop doesn't fall over).


"Markw911" wrote:

Hello,
I need to create a word doc using a word template with merge fields in it.
I would like to have a button in excel that launches the word template and
populates it with data that is stored in various sheets of my excel workbook.
Is this fairly straight forward and does anyone have sample code for
populating a word template from excel?
Thank you,
Mark

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I link between a Word template and an Excel template Josh Excel Discussion (Misc queries) 0 April 1st 08 12:36 AM
Opening a Word Template from within Excel perry Excel Worksheet Functions 0 January 30th 07 12:23 PM
Opening MS Word template using VBA in excel Nieuwsgroepen Excel Programming 1 October 29th 06 03:40 PM
Sending from excel to word template pizdus Excel Discussion (Misc queries) 0 January 17th 06 05:57 PM
Excel VBA from Word Template chesney[_2_] Excel Programming 0 June 17th 04 09:32 AM


All times are GMT +1. The time now is 10:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"