5941 error with a bookmark

K

Kevin Porter

Hello,

I had a word document created by the person before me that has quit working.
I get the '5941' The requested member of the collection does not exist.

The one it debugs to is the PFNum option. The person that this was written
for would sometimes leave info in the form that popped up, she was instructed
then to debug and delete it manually in the program (once again before my
time). The last time this error started coming up. I honestly do not even no
were to look.

It is supposed to pop up a form that you fill in then it fills in the
letter. Any help would be appreciated. It seems odd it just stops on that
one. If I comment it off then I get the same error for the frmMain at the
bottom.


Private Sub Document_New()
frmMain.Show
End Sub

Private Sub Document_Open()

With ThisDocument
.Bookmarks("Name").Range.Text = ""
.Bookmarks("Address").Range.Text = ""
.Bookmarks("CSZ").Range.Text = ""
.Bookmarks("EffDates").Range.Text = ""
.Bookmarks("Agent1").Range.Text = ""
.Bookmarks("Agent2").Range.Text = ""
.Bookmarks("PFNum").Range.Text = ""
.Bookmarks("PolicyNum").Range.Text = ""
.Bookmarks("CheckNum").Range.Text = ""
End With

frmMain.Show
End Sub

Thanks,

Kevin
 
K

Kevin Porter

Figured it out. Loaded the script writer and it hadn't been given a name, or
the name had been deleted. Thanks anyways.
 
M

macropod

Hi Kevin,

I'd suggest a different approach, to minimise the risk of the bookmarks being deleted (and to report when they have been):

Option Explicit

Private Sub Document_New()
frmMain.Show
End Sub

Private Sub Document_Open()
Dim StrBkMrks As String, i As Integer, BmkNm As String, NewTxt As String
StrBkMrks = "Name,Address,CSZ,EffDates,Agent1,Agent2,PFNum,PolicyNum,CheckNum"
NewTxt = ""
With ThisDocument
For i = 0 To UBound(Split(StrBkMrks, ","))
BmkNm = Split(StrBkMrks, ",")(i)
Call UpdateBookmark(BmkNm, NewTxt)
Next
End With
frmMain.Show
End Sub

Sub UpdateBookmark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
If Documents.Count > 0 Then
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
ActiveDocument.Bookmarks.Add BmkNm, BmkRng
Else
MsgBox "Bookmark """ & BmkNm & """ not found."
End If
End If
Set BmkRng = Nothing
End Sub

With the above approach, your userform can also call the UpdateBookmark sub (passing to it the BmkNm and NewTxt parameters) to
update the text without the risk of deleting the associated bookmarks. Also, adding/deleting bookmarks for the code to process is as
easy as modifying the StrBkMrks variable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top