Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fill in WORD template fields using VBA??

Posted on 2006-11-09
7
Medium Priority
?
625 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 600 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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 

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 900 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

824 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