Solved

Fill in WORD template fields using VBA??

Posted on 2006-11-09
7
560 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:dkim18
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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 video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now