Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate Word 2000 Fields from Visual Basic 6?

Posted on 2004-04-01
11
Medium Priority
?
1,554 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 1280 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

598 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