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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using same procedure for feeding a variety of Word fields 1

Status
Not open for further replies.

Phil56

Programmer
May 22, 2001
5
GB
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
 
The error would occur on the line starting ".ActiveDocument", because the member is not found in the Bookmarks collection. Because the error occurs, the selection does not change. So when you Resume Next, you are returning to the ".Selection.Text=" statement, which replaces the wrong bookmark for the form field.

The ideal thing would be to "Resume Next+1", but of course you can't do that. An alternative would be to combine the selection of the bookmark text (or location) with changing it. You can't do that either--but you can do something that's probably just as good. Instead of first selecting and then replacing the text, just replace it without selecting it by using this statement:
.ActiveDocument.Bookmarks("lastname").Text = (CStr(Forms!WordPrint!lastname))

This will work provided you don't need to have the last text inserted remain selected. If you do, you'll have to leave it as two statements. In that case, your error routine will have to set a flag, and your ".Selection.Text=" statement will have to be in an If statement that tests the flag. Of course, you'll have to initialize the flag before each bookmark, too, so the code will expand and get rather intricate.
Rick Sprague
 
Rick

Many thanks for your clear explanation! I've now experimented further and by including the If/EndIf lines, as below for each field, it appears now to be working fine!

If ActiveDocument.Bookmarks.Exists("lastname") = True Then
.ActiveDocument.Bookmarks("lastname").Select
.Selection.Text = (CStr(Forms!WordPrint!lastname))
End If

Thanks again for your help.

Phil

 
Phil,

Your solution is far better than the complicated code I was suggesting. I had forgotten about the Bookmarks.Exists property. Glad you found it! Rick Sprague
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top