Run-Time Error 5941

G

Greg Maxey

I am trying to run a code I got from the Word VBA FAQ page:
Sub SymbolCarousel()

Select Case Selection.Fields(1).Code.Characters(29)
Case "Y"
Selection.Fields(1).Code.Characters(29) = "N"
Case "N"
Selection.Fields(1).Code.Characters(29) = "?"
Case "?"
Selection.Fields(1).Code.Characters(29) = "Y"
Case Else
End Select

End Sub



I have looked at this before and was able to make it work. For some reason
now, I keep getting a Run-Time Erro 5941 "The requested member of the
collections does not exist." This has me a bit baffled. I don't know what
a Run-Time error is really, or a member, or a colection in this case. Once
I was able to get the macro to step through and make the change after I had
toggled the display of the field codes in the document. This worked only
once however and now I get the error regardless of what I do.

A few weeks ago I made an Option Explicit statement in my module. I removed
it but, no joy.

This macro is obviously well tested or it wouldn't be of the FAQ page. Can
anyone help out and let me know what setting I must have monkey'd with to
mess this up.



Thanks.
 
S

Shauna Kelly

Hi Greg

The SymbolCarousel code is designed to be run from a macrobutton field. It's
designed to run when the macrobutton field is double-clicked (or just
clicked, depending on a setting that you can change using
Application.Options.ButtonFieldClicks = 1). If a macrobutton field has just
been clicked, then the selection must necessarily include a field. So
Selection.Fields(1) will return the first field in the selection. (And if a
macrobutton field has just been clicked there will only be one field, so
Selection.Fields(1) must be the macrobutton field you just clicked.)

For Bill's code to work, the macrobutton field has to start out looking
something like { macrobutton SymbolCarousel Y }.

If you wanted to add some error checking to prevent running the code unless
a field was selected, then you could do

Sub SymbolCarousel()

If Selection.Fields.Count = 0 then
msgbox "No field is selected"
Else
'.... rest of the code here
End If

End Sub

To respond to some of the other questions:

Two things happen to Word VBA code. First, the code compiles. Then it runs.
You can get a compile-time error if you have a syntax error which VBA can't
understand. For example, if you wrote ActiveDocument.blablabla = 2, then
Word would, quite rightly, not understand and you'd get a compile-time
error. Once your code has survived compiling, it can run. You'll get a
run-time error when something goes wrong. Often, it's because the code has
referred to something that doesn't exist. For example,
ActiveDocument.Range.Text = "Hello world" will compile, because it's valid
code. But you would get a run time error if that code ran when no document
was open.

The word object model is, in general, hierarchical. And, broadly, every
object is based, ultimately, on the Word.Application object. Sometimes VBA
will assume Word.Application so you don't have to type it out. So you can
type just Documents.Count, but that really means
Word.Application.Documents.Count. When we see it in those terms, then
"Documents" here is more easily seen as a property of the Application. And
the Documents property returns a collection of documents. So
Word.Application.Documents means "give me all the documents in the Word
application". And Count is a property of *the collection* (not a property of
an individual document).

So a Collection is, well, a collection, just as you might have a collection
of CDs or a collection of socks. And (I'm fudging a bit here, but never
mind) each element in (or member of) a collection is of the same kind. So
each member of your Socks collection is a sock, and each member of the
Documents collection is a Document.

Since Documents is a collection of several (ie 0 to many) documents, then we
need a way to get to an individual document. So
Word.Application.Documents(1) refers to the first document. And
Word.Application.Documents(1).Paragraphs will return a collection of all the
paragraphs in the first document. And
Word.Application.Documents(1).Paragraphs(1) will return the first paragraph
in the first document, which demonstrates the hierarchical nature of the
thing.

A Document obviously has lots of properties (eg the AttachedTemplate) and
methods (eg PrintOut). But the collection of Documents also has properties
(eg Count) and methods (eg Add), which apply to the collection of all
documents.

If the SymbolCarousel code wasn't running, then it probably fell over on the
line
Select Case Selection.Fields(1).Code.Characters(29)

That would happen if there were no Fields in the Selection. That is, if the
Fields collection in the Selection had no members, then you'd get an error
saying that the requested member (number 1) of the collection (of Fields in
the Selection) doesn't exist.
A few weeks ago I made an Option Explicit statement in my module. I
removed
it but, no joy.
Leave it in your module, and put it into all modules, because it helps
prevent you from making mistakes by requiring you to declare all variables
before you use them. Since the SymbolCarousel code doesn't use any
variables, Option Explicit would not affect it anyway.

For what it's worth, Bill's code does work, and I've used it a lot. I quite
like changing the font of the macrobutton field to Wingdings, changing the
"N", "?" and "Y" as appropriate, and using it to toggle between a tick and a
cross.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg Maxey

Shauna,

This was wierd. I used your macro and carefully constructed the field.
Still no joy. If I double clicked the NO I go error "The requested member
of the collection does not exist". If I tried to run the code from the VBE
I got "Run Time Error 5941 "Same message." This time however, if I toggled
the display of the the field code in the document, then the Y, N, ? would
carousel. I could then go back and toggel field codes. Next, I moved the
code from the document project to my normal.dot, still no joy. Finally I
created a new document and called the code from the normal.dot. It worked!
I then cut the code from normal.dot and put it in the document project. It
still works.

Whatever the problem was, I hope it was a one time gremlin and gone. I will
have a hard look at your other tips. Thanks
 

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