Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail Merging to Word from VB6 app

Status
Not open for further replies.

StuH

Programmer
Mar 25, 2001
53
AU
Hi guys,

Can someone point me to some doco or give me some pointers please:

I have a VB6 form where users enter/store data. I need to be able to click a button (or choose a menu option) which will open MS Word (97/2000/XP whatever), open a particular document and merge data fields into the document to create a form letter.

I also need to know how to link to Word in order to construct the form letter in the first place - ie. how to nominate data fields from my app for selectable insertion by users to create the mask in the first place.

Any ideas? Thanks in advance.

Stu.
 
Try using something like this: Its complete and it works
Generate your word doc in Word and put in named bookmarks (docno, user etc)where you want your input to be. This routine also shows how to print tables from a listview.
Code:
Private Sub cmdPrint_Click()
 Dim page$, pageno
 Dim temp, temp1 As Integer
 page$ = reorder$
Dim docname As String
Dim myrange
Dim mydoc As Document
Dim mystr As String
Me.MousePointer = vbHourglass
setupword ("Reorderlist.doc")  'setupwordfunction module 
With wordapp
.Visible = False
End With
Set mydoc = ActiveDocument
Set myrange = ActiveDocument.Bookmarks("docno").Range
myrange.InsertAfter page$
Set myrange = ActiveDocument.Bookmarks("user").Range
myrange.InsertAfter username$
mystr = mystr & "Description$Shortage$Ave.Price$Main Supplier" & vbCrLf
For temp1 = 1 To loop1 - 1
mystr = mystr & Me.lvwParts.ListItems(temp1).Text & "$" & Me.lvwParts.ListItems(temp1).SubItems(1) & "$" & Me.lvwParts.ListItems(temp1).SubItems(2) & "$" & Me.lvwParts.ListItems(temp1).SubItems(3) & "$" & vbCrLf
Next temp1
Set myrange = mydoc.Bookmarks("list").Range
myrange.InsertAfter mystr
With myrange
    .ConvertToTable Separator:="$", _
        NumColumns:=5, Format:=wdTableFormatGrid7, ApplyBorders:=True _
        , ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows _
        :=False, ApplyLastRow:=False, ApplyFirstColumn:=False, ApplyLastColumn:= _
        False, AutoFit:=True
End With
mydoc.PrintOut
For temp1 = 1 To 16
For temp = 1 To 10000000
Next temp
DoEvents
Next temp1
Me.MousePointer = vbDefault
MsgBox "Printing"
closeword
End Sub
You also need the functions setupword and closeword:
These will be in a module:
Code:
Sub setupword(docname)
Set worddoc = wordapp.Documents.Open(App.Path & "\" & docname)
Dim mydoc As Word.Document
End Sub
Sub closeword()
wordapp.Documents(1).Close (wdDoNotSaveChanges)
End Sub
 
Hi John,

Thanks for this. Is it posted at a website somewhere with more detail?

With the creation of the word doc mask file, do you think it's possible to open a master doc file in word with a window from my app always on top which offers the various bookmarks available to be inserted into the doc? i.e. place the cursor in the doc where you want the selected bookmark to go, and double-click the desired one from the app's window?

Does this make sense?

Thanks,

Stu.
 
Hi Stu

Sorry but I haven't tried to reference backwards from Word. Setting wordapp.visible=true will show the working doc. A quick fix might be to use the clipboard. You can load the clipboard from code eg:

Code:
Clipboard.Clear
Clipboard.SetText "Anytext"

and paste into Word

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top