Solved

Populate Word 2000 Fields from Visual Basic 6?

Posted on 2004-04-01
11
1,547 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

717 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