Populate Word 2000 Fields from Visual Basic 6?

I have a MS-Word Template which has various fields (similar to mail merge fields.)

- I want to open MS-Word
- Populate the fields with variables from my VB6 app
- Save the file as one of the variables (i.e. account number) to a temporary directory

(I will then attach the file to an Outlook e-mail and send it to a recipient, then delete the temporary file... I can figure this part out on my own though.)


I was looking at this solution over here: http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20092104.html?query=merge+word+document&searchType=topic

He uses bookmarks.  I'm thinking this is the same as what I'd have to do.  I understand most of the code.. but I'm not sure what "Private Sub SetBookmark" is for.. ?

I'm assuming I assign the different values in the Word Doc with the following line:
SetBookmark docLetter, "Bookmark Name", "Value

NC
LVL 1
NO_CARRIERAsked:
Who is Participating?
 
MitzsConnect With a Mentor Commented:
Hi you can use fields of word  instead of bookmarks.

Open a word document and goto Insert---> Fields--->(Categories)Document Automation--->(FieldNames)DocVariables---> (Field Codes) DOCVARIABLE "Field Name"

Insert 2 fields like this and save the file  as test.doc in your C:\.

Open a new project
Add reference to Microsoft word Object xx.0 Library and a command button (command1) on form and run this code and it should show you a word document with test data.


Private Sub Command1_Click()
    Dim objWdApp As Word.Application
    Dim objWdDoc As Word.Document
    Dim objwdRange As Word.Range
   
    'Create instance of Word Application
    Set objWdApp = New Word.Application
    Set objWdDoc = objWdApp.Documents.Add("C:\Test.doc")
   
   
    'Set the range equal to the entire word doc
    Set objwdRange = objWdDoc.Range

    With objwdRange
        For i = 1 To .Fields.Count
            If .Fields(i).Type = wdFieldDocVariable Then
                .Fields(i).Result.Text = i & " - Test Data"
            End If
        Next i
    End With
    objWdApp.Visible = True
End Sub


hope this helps

Mitzs

0
 
sokolovskyCommented:
>I understand most of the code.. but I'm not sure what "Private Sub SetBookmark" is for.. ?
Sub SetBookmark is to fill the bookmark with name strBookmark in WordDoc with value strText.

>I'm assuming I assign the different values in the Word Doc with the following line:
>SetBookmark docLetter, "Bookmark Name", "Value"
Yes. If you have set 2 bookmarks  - "FiledOne" and "TotalSum" in your word doc,
then call:
SetBookmark docLetter, "FieldOne", "This is a value for field One"
SetBookmark docLetter, "TotalSum", "The total sum is 100"
0
 
MrPanCommented:
I use this technique as well. What you do is create a bookmark in word this acts as a place holder.

This is useful as you can insert text anywhere (ie in a form)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
NO_CARRIERAuthor Commented:
sounds good.. I'll try it out.
Do you happen to know the command to save in Word (and specify a filename)?
0
 
sokolovskyCommented:
Save or SaveAs
Document object have sub Save and saveAs
Sub SaveAs([FileName], [FileFormat], [LockComments], [Password], [AddToRecentFiles], [WritePassword], [ReadOnlyRecommended], [EmbedTrueTypeFonts], [SaveNativePictureFormat], [SaveFormsData], [SaveAsAOCELetter])
Look.

      Set docLetter = appWord.Documents.Open(App.Path & "\" & strDocument)
docLetter.save
docLetter.saveAs("test.doc")
docLetter.saveAS("test.txt",wdFormatText)

wdFormatText=2
wdFormatDocument=0
wdFormatDOSText=4
wdFormatHTML=8
wdFormatRTF=6
wdFormatTemplate=1
wdFormatUnicodeText=7
0
 
MitzsCommented:
oh..almost forgot

if you want all this to happen in background without showing to user then you can remove this line
objWdApp.Visible = True

and you can save the file as

objWdDoc.SaveAs "C:\test" & accountnumber & ".doc"

objwdapp.Quit

 Set objwdRange = Nothing
    Set objWdDoc = Nothing
    Set objWdApp = Nothing

Mitzs
0
 
MitzsCommented:
oh...god what am i doing.... again...

use to save the temp. file to temporary directory

objWdDoc.SaveAs "C:\TempDir\" & accountnumber & ".doc"

Mitzs
0
 
NO_CARRIERAuthor Commented:
Mitzs... wow, works perfect.. and very efficient too.

How do I reference the different fields though?  It's currently populating each field with "1 - Test Data", "2 - Test Data", etc.

Say I have the field names:

ClientInformation
AccountNumber
AddressLine1

how would I reference them individually?
0
 
NO_CARRIERAuthor Commented:
got it working.. not just trying out the saving part..
0
 
NO_CARRIERAuthor Commented:
Very simple solution.. must more efficient than the code I was previously using (utilizing bookmarks).

Thx/NC
0
 
NO_CARRIERAuthor Commented:
how do I open the word document non-exclusive?.. (shared mode).

currently if more than 1 person is performing the function at once it gives the error "cannot open.. document open elsewhere"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.