Mikey F
02-17-2002, 01:41 AM
Hi
Is there a way of automatically merging an address record into a Word (letter) template from a command button on a form.
Ideally I like to be able to print the letter as well so the user has no contact with MS Word.
Thanks, Mike
jwindon
02-17-2002, 07:32 AM
You could try your hand at creating a report that looks like a letter. Or you could use mail merge or bookmarks in word. This code uses bookmarks. (one letter only)
Private Sub MergeButton_Click()
' Open a memo in Word and insert text - used by menu command.
Dim dbs As DAO.Database, rstNames As Recordset
'switch to ms word so it won't go away when you finish.
Dim appWord As Word.Application
DoCmd.GoToControl "FirstName"
' Start Microsoft Word 97.
Set appWord = CreateObject("Word.Application")
With appWord
' Make the application visible.
.Visible = True
' Open the document.
.Documents.Open ("c:\my documents\DataMailMerge.doc")
' Move to each bookmark and insert text from the form.
.ActiveDocument.bookmarks("FirstName").Select
.selection.Text = (CStr(Forms!frmForm1!FirstName))
.ActiveDocument.bookmarks("LastName").Select
.selection.Text = (CStr(Forms!frmForm1!LastName))
End With
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word 97 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub
MergeButton_Err:
' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.selection.Text = ""
Resume Next
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
On Error Resume Next
AppActivate "Microsoft Word"
'if word isn't running, start and activate it
If Err Then
Shell "C:\Program Files\Microsoft Office\Office\" & "WinWord / Automation", vbMaximizedFocus
AppActivate "Microsoft Word"
End If
On Error GoTo 0
End Function