CommandBars reference error

W

Wraithchilde

I hope this is a double post. The forum burped right in the middile of my
first attempt and I waited way more than 5 minutes to see if it showed up.
Anyway...

Here is the problem. I have, for example, Document1 which has Toolbar1.
Somewhere down the line it opens Document2 (or 3 or 4 etc) which has
Toolbar2. If I make any reference to Toolbar1 I get an invalid procedure call
error. This happens whether or not I use Application.CommandBars("Toolbar1")
instead of ActiveDocument.CommandBars("Toolbar1"). You would think there
would be a difference. I'm hoping that adding all the toolbars to all the
documents isn't the only solution.
 
J

Jean-Guy Marcil

Wraithchilde was telling us:
Wraithchilde nous racontait que :
I hope this is a double post. The forum burped right in the middile
of my first attempt and I waited way more than 5 minutes to see if it
showed up. Anyway...

Here is the problem. I have, for example, Document1 which has
Toolbar1. Somewhere down the line it opens Document2 (or 3 or 4 etc)
which has Toolbar2. If I make any reference to Toolbar1 I get an
invalid procedure call error. This happens whether or not I use
Application.CommandBars("Toolbar1") instead of
ActiveDocument.CommandBars("Toolbar1"). You would think there would
be a difference. I'm hoping that adding all the toolbars to all the
documents isn't the only solution.

Are these Toolbars in documents or templates?

Where is the code calling Toolbar1 located?

Show us the *relevant* code you use.

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

Wraithchilde

Thanks for the reply.

This is the only relevant code. I reference toolbar1 from document2 as

Application.CommandBars("<toolbar name>").Visible = [True, False]

I hide it because I don't want them to be able to start a new process until
they complete this one. So if I hide it after they start the process I need
to unhide it when it's complete. I only know it's complete in the final
document.

I suppose I could call some other sub in a module in the original document
to do something but I'd have to wait until all other documents are closed
which doesn't really solve the problem. And also leaves the question, "Why
doesn't Application know about all the CommandBars" unanswered.
 
J

Jean-Guy Marcil

Wraithchilde was telling us:
Wraithchilde nous racontait que :
Thanks for the reply.

This is the only relevant code. I reference toolbar1 from document2 as

Application.CommandBars("<toolbar name>").Visible = [True, False]

I hide it because I don't want them to be able to start a new process
until they complete this one. So if I hide it after they start the
process I need to unhide it when it's complete. I only know it's
complete in the final document.

I suppose I could call some other sub in a module in the original
document to do something but I'd have to wait until all other
documents are closed which doesn't really solve the problem. And also
leaves the question, "Why doesn't Application know about all the
CommandBars" unanswered.

You still haven't answered all my questions.. without which I cannot test
your situation on my machine...

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

Wraithchilde

The window crapped out on me again. Sorry if this posts double.

If you are trying to reproduce the error just do this:

Open a template with a toolbar. Any template will do. Open a new document
and add the code Application.CommandBars("<name of toolbar in first
template>").Visible = False. That should do it.

But to try to answer all the questions, the toolbar is a template (like
toolbar.dot) in the user's startup folder. It opens a document, which is also
a template but opens as Document1, 2, 3, etc. The above code is in this
second document. Di I get them all this time?
 
J

Jean-Guy Marcil

Wraithchilde was telling us:
Wraithchilde nous racontait que :
The window crapped out on me again. Sorry if this posts double.

If you are trying to reproduce the error just do this:

Open a template with a toolbar. Any template will do. Open a new
document and add the code Application.CommandBars("<name of toolbar
in first template>").Visible = False. That should do it.

If you just open a template, then create a document based on another
template (like Normal.dot), then you will not be able to manipulate the
Toolbars in the opened template from this new document.
But to try to answer all the questions, the toolbar is a template
(like toolbar.dot) in the user's startup folder. It opens a document,
which is also a template but opens as Document1, 2, 3, etc. The above

If the thing you open is named "Document1" then you did not open a template,
you created a document based on a template (i.e. it cannot be a document
that is also a template).
code is in this second document. Di I get them all this time?

If the template is loaded when Word Starts, then you will be able to
manipulate its toolbars. The following works for me (if the toolbar is in a
template that is in the Start-up folder):

Application.CommandBars("<name of toolbar in loaded template>").Visible
= True

or
Dim myCmd As CommandBar

Set myCmd = Application.CommandBars("<name of toolbar in loaded
template>")
myCmd.Visible = False

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

Wraithchilde

Thanks for taking the time to answer. Unfortunately it's not a solution for
me. I suppose it's something Microsoft devs needs to look into. If the
CommandBar is not a member of Application then what IS it a member of?
 
J

Jean-Guy Marcil

Wraithchilde was telling us:
Wraithchilde nous racontait que :
Thanks for taking the time to answer. Unfortunately it's not a
solution for me. I suppose it's something Microsoft devs needs to
look into. If the CommandBar is not a member of Application then what
IS it a member of?

It can also be a member of Document.

If a document based on Template A is created and Template A has a custom
toolbar, when you switch to a document based on Template B (which does not
have the custom toolbar) then the custom toolbar from Template A
automatically becomes invisible (as it is not available to documents not
created from Template A). So, in those cases, you do not need to have code
to make that toolbar invisible.

If, on the other hand, the toolbar is in a global template (located in the
Word Stat-up folder), then that toolbar is available to all documents and
can be manipulated at will from any documents.

So, again, what is your problem? It seems to me that you do not really need
to set the toolbar visible status to False, unless I have misunderstood your
situation.

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

Wraithchilde

So, again, what is your problem? It seems to me that you do not really need
to set the toolbar visible status to False, unless I have misunderstood your
situation.

That IS the problem. There are times when I need to set visible to false and
times I don't need to set visible to false. It's the times when I don't need
to set it that it blows up. There's no way to catch it because you can't even
check that the toolbar is there or not without causing an error. Not even
with IsObjectValid(). So I ended up just wrapping all of them in on error
resume next, on error goto 0. To me it's like a hack solution.
 
J

Jean-Guy Marcil

Wraithchilde was telling us:
Wraithchilde nous racontait que :
That IS the problem. There are times when I need to set visible to
false and times I don't need to set visible to false. It's the times

What are those conditions that determines the toolbar visibility status?
What test does the code perform to know if the target toolbar should be
displayed or not?
when I don't need to set it that it blows up. There's no way to catch
it because you can't even check that the toolbar is there or not

Yes you can. With a loop you can check if a particular toolbar is available
or not. If it is not available, it is invisible (so, as I stated before, no
code needed to set its visibility status) as it does not exists in that
particular context.
without causing an error. Not even with IsObjectValid(). So I ended
up just wrapping all of them in on error resume next, on error goto
0. To me it's like a hack solution.

Again, if the code that you are using is activated from Document1 based on
template B but references a toolbar from template A, the toolbar will always
be unavailable (and invisible), so you do not need to code for that.

Can you explain again, not stating what you are trying to do with your code,
but the overall situation. Because, from your own words:
<quote>
But to try to answer all the questions, the toolbar is a template (like
toolbar.dot) in the user's startup folder. It opens a document, which is
also
a template but opens as Document1, 2, 3, etc. The above code is in this
second document. Di I get them all this time?
<unquote>

Notwithstanding the fact that it is difficult to understand what you mean by
"which is also a template but opens as Document1" (are you opening the
template itself or creating a document based on that template? I will assume
the latter.); your code should not ever throw an exception error. If the
template is truly in the user's start-up folder, then the toolbar will
always be accessible. If the code throws an error, then it means the
template is not loaded, there is a name error in the code or a coding error.

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

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