I am creating an Access database for the department I work in. When reviewing orders on a form, there are certain instances were we need to mail a letter out to the customer. I have a command button that opens up a word form and inserts address info from the customer table in access to the word document in this format:
Name
Address
City, State, Zip.
I am coming across an issue were only the City, State, Zip remain in the word document after the VBA is finished. I can see it while its processing and it does insert the Name and Address, but for some reason it deletes the Name and Address leaving only the City, State, Zip. I am having difficulty figuring out why this is happening and how to prevent it. I tried using this code to fix the issue, but the code seems to stop after it reaches this:
.ActiveDocument.Bookmarks("Name").Select
.Selection.Text = (CStr(Forms!frm_Main!CHName))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Last",Range:=Selection.Range
Here is my VBA Code, any help with this would be greatly appreciated.
Private Sub cmdElan_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Copy the Photo control on the frm_Main form.
'DoCmd.GoToControl "Photo"
'DoCmd.RunCommand acCmdCopy
'Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("C:\Users\c015892\Desktop\Elan.doc")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("Name").Select
.Selection.Text = (CStr(Forms!frm_Main!CHName))
.ActiveDocument.Bookmarks("Address").Select
.Selection.Text = (CStr(Forms!frm_Main!CHStreet))
.ActiveDocument.Bookmarks("City").Select
.Selection.Text = (CStr(Forms!frm_Main!CHCity))
.ActiveDocument.Bookmarks("State").Select
.Selection.Text = (CStr(Forms!frm_Main!CHState))
.ActiveDocument.Bookmarks("Zipcode").Select
.Selection.Text = (CStr(Forms!frm_Main!CHZip))
'.ActiveDocument.Bookmarks("Greeting").Select
'.Selection.Text = (CStr(Forms!frm_Main!FirstName))
'Paste the photo.
'.ActiveDocument.Bookmarks("Photo").Select
'.Selection.Paste
End With
'Print the document in the foreground so Microsoft Word 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 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
'If the Photo field is empty.
'ElseIf Err.Number = 2046 Then
'MsgBox "Please add a photo to this record and try again."
'Else
'MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
Name
Address
City, State, Zip.
I am coming across an issue were only the City, State, Zip remain in the word document after the VBA is finished. I can see it while its processing and it does insert the Name and Address, but for some reason it deletes the Name and Address leaving only the City, State, Zip. I am having difficulty figuring out why this is happening and how to prevent it. I tried using this code to fix the issue, but the code seems to stop after it reaches this:
.ActiveDocument.Bookmarks("Name").Select
.Selection.Text = (CStr(Forms!frm_Main!CHName))
'Add this line to reapply the bookmark name to the selection.
.ActiveDocument.Bookmarks.Add Name:="Last",Range:=Selection.Range
Here is my VBA Code, any help with this would be greatly appreciated.
Private Sub cmdElan_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Copy the Photo control on the frm_Main form.
'DoCmd.GoToControl "Photo"
'DoCmd.RunCommand acCmdCopy
'Start Microsoft Word 97.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("C:\Users\c015892\Desktop\Elan.doc")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("Name").Select
.Selection.Text = (CStr(Forms!frm_Main!CHName))
.ActiveDocument.Bookmarks("Address").Select
.Selection.Text = (CStr(Forms!frm_Main!CHStreet))
.ActiveDocument.Bookmarks("City").Select
.Selection.Text = (CStr(Forms!frm_Main!CHCity))
.ActiveDocument.Bookmarks("State").Select
.Selection.Text = (CStr(Forms!frm_Main!CHState))
.ActiveDocument.Bookmarks("Zipcode").Select
.Selection.Text = (CStr(Forms!frm_Main!CHZip))
'.ActiveDocument.Bookmarks("Greeting").Select
'.Selection.Text = (CStr(Forms!frm_Main!FirstName))
'Paste the photo.
'.ActiveDocument.Bookmarks("Photo").Select
'.Selection.Paste
End With
'Print the document in the foreground so Microsoft Word 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 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
'If the Photo field is empty.
'ElseIf Err.Number = 2046 Then
'MsgBox "Please add a photo to this record and try again."
'Else
'MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub