I've used Access for sometime now, but ashamedly am only recently getting to grips (or not!) with VBasic.
If anyone can help with the problem I'm struggling with below I'd be really grateful!
I'm developing an Access97 database which can draw data from form fields, open a Word document, replace the Word bookmarks with the data, print word doc, before closing Word.
I'm hoping to use the same code with all possible data that would be required for a variety of Word docs made available through a listbox to the user.
I can't work out the line of code I need to include .. to cope with the error 5941 'The requested member of the collection does not exist.'
This occurs when there are fewer Word bookmarks than the data fields in the Access form. So I need error handling to ignore those lines for where there is no corresponding bookmark.
I have pasted part of the code below, with what I assume as the offending code highlighted. If you can imagine say, 20 odd pieces of available data, with only 4 being required by the selected Word document - I need the other 16 ignored. The way the code is at the moment is that a 'redundant' CStr is wrongly inserted into the last named bookmark, despite the err code to Resume Next as below ..
.ActiveDocument.Bookmarks("firstname".Select
.Selection.Text = (CStr(Forms!WordPrint!firstname))
.ActiveDocument.Bookmarks("lastname".Select
.Selection.Text = (CStr(Forms!WordPrint!astname))
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
' Close printscreen and update main screen
DoCmd.RunMacro "Corresprintfinal"
Exit Sub
formload_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 a bookmark is absent
'continue to next bookmark named in code
ElseIf Err.Number = 5941 Then
Resume Next
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
Many thanks to anyone who can help with this!
Phil
If anyone can help with the problem I'm struggling with below I'd be really grateful!
I'm developing an Access97 database which can draw data from form fields, open a Word document, replace the Word bookmarks with the data, print word doc, before closing Word.
I'm hoping to use the same code with all possible data that would be required for a variety of Word docs made available through a listbox to the user.
I can't work out the line of code I need to include .. to cope with the error 5941 'The requested member of the collection does not exist.'
This occurs when there are fewer Word bookmarks than the data fields in the Access form. So I need error handling to ignore those lines for where there is no corresponding bookmark.
I have pasted part of the code below, with what I assume as the offending code highlighted. If you can imagine say, 20 odd pieces of available data, with only 4 being required by the selected Word document - I need the other 16 ignored. The way the code is at the moment is that a 'redundant' CStr is wrongly inserted into the last named bookmark, despite the err code to Resume Next as below ..
.ActiveDocument.Bookmarks("firstname".Select
.Selection.Text = (CStr(Forms!WordPrint!firstname))
.ActiveDocument.Bookmarks("lastname".Select
.Selection.Text = (CStr(Forms!WordPrint!astname))
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
' Close printscreen and update main screen
DoCmd.RunMacro "Corresprintfinal"
Exit Sub
formload_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 a bookmark is absent
'continue to next bookmark named in code
ElseIf Err.Number = 5941 Then
Resume Next
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
End Sub
Many thanks to anyone who can help with this!
Phil