Solved

Populate Word 2000 Fields from Visual Basic 6?

Posted on 2004-04-01
11
1,540 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now