Macro Code Problem

C

caring4211

I've written the following code and now I'm receiving a runtime error message
for "runtime error 5941" "the requested member of the collection does not
exist"

Can someone tell me what I'm doing wrong? Here is the code:

Set vOne = ActiveDocument.FormFields("bkone").checkbox

If vOne.Value = True Then
ActiveDocument.Bookmarks("bkOneTwo").Select
End If

Please advise!
 
D

Dave Lett

Hi,

You can test if the bookmarks exist in code with something like the
following:

MsgBox "bkOneTwo exists:" & vbTab &
ActiveDocument.Bookmarks.Exists(Name:="bkOneTwo")
MsgBox "bkone exists:" & vbTab &
ActiveDocument.Bookmarks.Exists(Name:="bkone")

Also, to really help with the issue, it might be useful to know which line
your routine is raising the error.

HTH,
Dave
 
J

Jean-Guy Marcil

caring4211 was telling us:
caring4211 nous racontait que :
I've written the following code and now I'm receiving a runtime error
message for "runtime error 5941" "the requested member of the
collection does not exist"

Can someone tell me what I'm doing wrong? Here is the code:

Set vOne = ActiveDocument.FormFields("bkone").checkbox

If vOne.Value = True Then
ActiveDocument.Bookmarks("bkOneTwo").Select
End If

On which line does the debugger stops?

Do you have multiple documents opened at the same time? If so, do not use
ActiveDocument, it may be pointing to the wrong document. Use Document
object variables instead:

Dim sourceDoc As Document
Dim vOne As CheckBox
Set sourceDoc = Documents("Document1") 'Actual name of document

Set vOne = sourceDoc.FormFields("bkone").CheckBox

If vOne.Value = True Then
sourceDoc.FormFields("bkOneTwo").Select
End If



--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

caring4211

The debugger stops on the first line. I only have one document opened but
I'll try renaming it to the document's actual name.

Lynda
 
J

Jean-Guy Marcil

caring4211 was telling us:
caring4211 nous racontait que :
The debugger stops on the first line. I only have one document
opened but I'll try renaming it to the document's actual name.

If you have that error on
Set vOne = ActiveDocument.FormFields("bkone").checkbox
this means there are no formfields named "bkone".

Double click on your formfield in design mode to access its properties, look
at the Bookmark field in the properties dialog and make sure it is indeed
"bkone".

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

caring4211

Thank you so much. That was exactly it! I didn't realize that they had to
have the "bk" portion before the "one" or "onetwo" in the properties. It's
working now!!!

THANK YOU!

Lynda
 

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