Solved

Fill in WORD template fields using VBA??

Posted on 2006-11-09
7
576 Views
Last Modified: 2009-03-07
Hi,
I am trying to find out if I can fill in fields of WORD template using VBA.

Say I have a template like this:

1) Project Num: [ClickHere to type]
2) Last Name: [ClickHere to type]
3) First Name: [ClickHere to type]
4) Summary: [ClickHere to type]
... etc..

the data is coming from the EXCEL sheet.
column1       column2             column3            column4
project#       lastName            firstName          Summary


I am not sure if this can be done.
Thanks much.
0
Comment
Question by:dkim18
  • 3
  • 3
7 Comments
 
LVL 15

Assisted Solution

by:cquinn
cquinn earned 200 total points
ID: 17907121
Instead of using fields insert bookmarks around where you want the details to go, then add the following code to your module

Public Sub SetBookmarkContent(sBookmark As String, sVarname As String)
Dim bkRange As Word.Range
On Error GoTo ErrTrap

    If ActiveDocument.Bookmarks.Exists(sBookmark) Then
        Set bkRange = ActiveDocument.Bookmarks(sBookmark).Range
        bkRange.Text = sVarname
        ActiveDocument.Bookmarks.Add Name:=sBookmark, Range:=bkRange
    End If

    Exit Sub
   
ErrTrap:
    End
End Sub


Call it like this:


    SetBookmarkContent "BookMarkName", "The text you want to appear there"
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17907777
This might be a candidate for MailMerge.

Set up a Main document in Word using the Mail Merge wizard (from the Tools Menu) with your spreadsheet as the datasource.

When that is done it will look like this:

1) Project Num: <<project# >>
2) Last Name: <<lastName>>
3) First Name: <<firstName>>
4) Summary: <<Summary>>

Then you should be able to click on the 'Merge to New Document' button on the Mail Merge toolbar and it will produce a page for each line in your spreadsheet.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17907886
Another alternative would be to use a table. You don't have to have borders if you don't want to see them.

Sub FillTable()
   
   Dim tbl As Table
   Set tbl = Doc.Tables(1)
   tbl.Cell(1, 2).Range.Text = "12345"
   tbl.Cell(2, 2).Range.Text = "Smith"
   tbl.Cell(3, 2).Range.Text = "John"
   tbl.Cell(4, 2).Range.Text = "This is my summary"
   
End Sub
0
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.

 

Author Comment

by:dkim18
ID: 17907932
Thanks guys.

cquinn,

Can you show me a little more detail please?

I have a userform from the EXCEL which let user to enter one row # to create a report.
Each row has above data.
Say the template name is ProjectName.dot
The report name will be lastname_firstname_projectnum.doc.

I don't know how to load the template and save it as .doc file.
I used WORD.Application and WORD.document to create the word document.
I am not quite sure how to use the template and save it as doc file.
Thanks much.
0
 

Author Comment

by:dkim18
ID: 17907957
GrahamSkan,

I must have misdirected you.
I have one excel spreadsheet and one word template.

Basically user will select the row to create a word document by filling in that template if that can be done.
The word doc will be saved under lastname_firstname_projectnum.doc.
0
 

Author Comment

by:dkim18
ID: 17907966
When the row # is selected, I will extract the data from the excel and hopefully fill in those onto the word document.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 total points
ID: 17910660
This illustrates how to get the data into a table that is already in the Word template:

Option Explicit

Sub WriteWordDoc()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim rng As Excel.Range
    Dim tbl As Word.Table
    Dim i As Integer
   
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Add("C:\MyTemplates\MyTemplate")
    Set tbl = wdDoc.Tables(1)
    Set rng = Application.Selection
   
    For i = 1 To 4
        tbl.Cell(i, 2).Range.Text = rng.Cells(1, i)
    Next i
   
End Sub
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

713 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