Solved

Populate Word 2000 Fields from Visual Basic 6?

Posted on 2004-04-01
11
1,543 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:NO_CARRIER
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:sokolovsky
ID: 10739082
>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
 
LVL 2

Expert Comment

by:MrPan
ID: 10739576
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
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10740671
sounds good.. I'll try it out.
Do you happen to know the command to save in Word (and specify a filename)?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 4

Expert Comment

by:sokolovsky
ID: 10740792
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
 
LVL 4

Accepted Solution

by:
Mitzs earned 320 total points
ID: 10741053
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
 
LVL 4

Expert Comment

by:Mitzs
ID: 10741129
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
 
LVL 4

Expert Comment

by:Mitzs
ID: 10741190
oh...god what am i doing.... again...

use to save the temp. file to temporary directory

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

Mitzs
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10741522
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
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10741806
got it working.. not just trying out the saving part..
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10742046
Very simple solution.. must more efficient than the code I was previously using (utilizing bookmarks).

Thx/NC
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10743233
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

Featured Post

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.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

840 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