phillbaker
Registered User.
- Local time
- Today, 19:41
- Joined
- Jun 29, 2008
- Messages
- 45
i have this form in which when you press a button it opens up word and using bookmarks pulls in the data from the form. But if someone hasnt filled in one of the fields it brings up invalid use of null when i press the button how can i stop this.
i have pasted the code below. can anyone help
The code to do this is below:
Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Copy the Photo control on the Employees form.
'DoCmd.GoToControl "Photo"
'DoCmd.RunCommand acCmdCopy
'Start Microsoft Word 2003.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("\\server\Database\MyMerge.doc")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms!Clients!CFirstName))
.ActiveDocument.Bookmarks("Last").Select
.Selection.Text = (CStr(Forms!Clients!CLastName))
'.ActiveDocument.Bookmarks("HouseName").Select
'.Selection.Text = (CStr(Forms!Clients![CHouse Name]))
.ActiveDocument.Bookmarks("Street").Select
.Selection.Text = (CStr(Forms!Clients!CStreet))
.ActiveDocument.Bookmarks("Village").Select
.Selection.Text = (CStr(Forms!Clients!CVillage))
.ActiveDocument.Bookmarks("PostalTown").Select
.Selection.Text = (CStr(Forms!Clients![CPostal Town]))
.ActiveDocument.Bookmarks("PostCode").Select
.Selection.Text = (CStr(Forms!Clients!CPostCode))
.ActiveDocument.Bookmarks("County").Select
.Selection.Text = (CStr(Forms!Clients!CCounty))
'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
i have pasted the code below. can anyone help
The code to do this is below:
Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Copy the Photo control on the Employees form.
'DoCmd.GoToControl "Photo"
'DoCmd.RunCommand acCmdCopy
'Start Microsoft Word 2003.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("\\server\Database\MyMerge.doc")
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms!Clients!CFirstName))
.ActiveDocument.Bookmarks("Last").Select
.Selection.Text = (CStr(Forms!Clients!CLastName))
'.ActiveDocument.Bookmarks("HouseName").Select
'.Selection.Text = (CStr(Forms!Clients![CHouse Name]))
.ActiveDocument.Bookmarks("Street").Select
.Selection.Text = (CStr(Forms!Clients!CStreet))
.ActiveDocument.Bookmarks("Village").Select
.Selection.Text = (CStr(Forms!Clients!CVillage))
.ActiveDocument.Bookmarks("PostalTown").Select
.Selection.Text = (CStr(Forms!Clients![CPostal Town]))
.ActiveDocument.Bookmarks("PostCode").Select
.Selection.Text = (CStr(Forms!Clients!CPostCode))
.ActiveDocument.Bookmarks("County").Select
.Selection.Text = (CStr(Forms!Clients!CCounty))
'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