Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fill in WORD template fields using VBA??

Posted on 2006-11-09
7
Medium Priority
?
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

670 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