Fill in WORD template fields using VBA??

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.
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cquinnCommented:
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
GrahamSkanRetiredCommented:
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
GrahamSkanRetiredCommented:
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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

dkim18Author Commented:
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
dkim18Author Commented:
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
dkim18Author Commented:
When the row # is selected, I will extract the data from the excel and hopefully fill in those onto the word document.
0
GrahamSkanRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.