AutoOpen Macro Not Showing Toolbar

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000

I have the following AutoOpen and AutoClose macros in the ThisDocument
module.

Private Sub AutoOpen()

CommandBars("Locates").Visible = True
BottomOfDocument = True

End Sub

Private Sub AutoClose()

CommandBars("Locates").Visible = False
SaveFile

End Sub

If the Locates toolbar is select on the View > Toolbars menu, then the above
macros work perfectly. However, if the Locates toolbar is NOT selected on
the View > Toolbars menu, then it will not be displayed the next time the
document is opened. Why?

I thought that by setting the Visible property = True in the AutoOpen macro,
that the toolbar would be displayed every time the document is opened.

FYI: The Locates toolbar is stored with the document and is not in
Normal.dot. Also, it should be available only to the specific document, and
not to any other document, which is why I turn it off with the AutoClose
macro.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
P

Peter

Try this:

CommandBars("Locates").Enabled = True
CommandBars("Locates").Visible = True

Although just toggling Visible should do the trick.

Also, if the toolbar is stored in that document, then it should be available only to that document and you shouldn't have to worry about hiding from others...

hth,

-Peter
 
J

Jean-Guy Marcil

Montana DOJ Help Desk was telling us:
Montana DOJ Help Desk nous racontait que :
Word 2000

I have the following AutoOpen and AutoClose macros in the ThisDocument
module.

Private Sub AutoOpen()

CommandBars("Locates").Visible = True
BottomOfDocument = True

End Sub

Private Sub AutoClose()

CommandBars("Locates").Visible = False
SaveFile

End Sub

If the Locates toolbar is select on the View > Toolbars menu, then
the above macros work perfectly. However, if the Locates toolbar is
NOT selected on the View > Toolbars menu, then it will not be
displayed the next time the document is opened. Why?

I thought that by setting the Visible property = True in the AutoOpen
macro, that the toolbar would be displayed every time the document is
opened.

FYI: The Locates toolbar is stored with the document and is not in
Normal.dot. Also, it should be available only to the specific
document, and not to any other document, which is why I turn it off
with the AutoClose macro.

You do not need to use Auto macros in the ThisDocument module. They are
deprecated. The only ones you might use are AutoExec and AutoExit in
add-ins.
Select "Document" in the dropdown list on the left at the top of the code
pane, as soon as you do a
Private Sub Document_New()

End Sub
module is added to the code pane. If you want an Open event, drop the list
on the right (at the top of the code pane) and choose in the event list (up
to Word XP there are 3, Word 2003 has 6).
Now that you have the Open even, you can delete the New event module.

Private Sub Document_Open()

CommandBars("Locates").Visible = True

End Sub

You do not need to hide it when you close the document because as soon as
your document is closed, the toolbar is gone (Or if you open another
document and then toggle document display between the two, the toolbar
should be visible only for the "container" document).
But you need to make it visible when opening a document. If you were working
with a template you would not need any code at all.

I do not know why your code fails. It should not, even if it is not
necessary.
What do you mean by
the above macros work perfectly. However, if the Locates toolbar is
NOT selected on the View > Toolbars menu, then it will not be
How can it be "selected"?

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

Montana DOJ Help Desk

Peter,

Thanks for the response. I tried your suggestion, but it didn't solve the
problem.

I realize that by storing the toolbar with the document (actually, it'll
probably end up going into a template), that the toolbar will not be
available to other documents. However, I wanted to make sure that it is
always visible when the document is opened--even if the user previously
turned it off--because the tasks that the document is used for really can't
be accomplished in an efficient manner without the toolbar, and since some
users would be utterly baffled without the toolbar, I wanted to make sure
that it is always visible. So that's why I want to make it visible when the
document opens.

As for why I'm hiding the toolbar when the document closes, to be honest, I
don't really remember why I set it up that way. I wrote that routine over a
year ago, and it probably seemed like a good idea at the time, but then
again, I didn't know a quarter of what I know now about VBA.

I'll give Jean's suggestion a try, and will post the results. Hopefully, I
will get the chance to work on this tonight, but it may be a couple of days.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
"Peter" <peterguy -at- hotmail -dot- com> wrote in message
Try this:

CommandBars("Locates").Enabled = True
CommandBars("Locates").Visible = True

Although just toggling Visible should do the trick.

Also, if the toolbar is stored in that document, then it should be available
only to that document and you shouldn't have to worry about hiding from
others...

hth,

-Peter
 
M

Montana DOJ Help Desk

Thanks for the reply. I tried your suggestion, and it worked. However,
after reading in Help, I'm a little confused on the difference between
AutoOpen and Document_Open. It seems like either should work in this
particular circumstance. I did see in Help that AutoOpen is global, whereas
Document_Open is not. So when would AutoOpen be used?

As for your other questions, what I mean by "the Locates toolbar is NOT
selected on the View > Toolbars menu" is that if you go to the View menu and
select Toolbars, there is no checkmark next to the toolbar. In other words,
a checkmark = selected (or visible), and no checkmark = not selected (or not
visible). Probably "selected" was not the best choice of words.

The SaveFile routine just saves the file. I have a routine for this because
there are certain things that I want to make sure are done before the file
gets saved. I wrote this routine before I found out about FileSave. I
could probably put all the code in the SaveFile routine into FileSave, but
at the time it was quicker to just have FileSave call SaveFile.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
C

Chuck

How would you ensure displaying Montana DOJ's toolbar without adding code to
a document? Doesn't storing macros in documents mean that they would travel
with the documents if they were emailed to another person/organisation?
Other people/organisations who may be (quite rightly) reluctant to open
documents containing macros, fearing malicious and/or uncontrollable code
being set loose on their systems without having first inspected the code to
satisfy themselves that it's not a problem. The specific code Montana DOJ is
talking about obviously isn't a problem, but as a general rule documents
containing macros are automatically suspect (at least anywhere I've ever
worked). Granted, the user has the option of "disabling macros" on document
open, but uneducated/careless/distracted users may click "enable macros"
instead, opening themselves to the possibility of unexpected results.
 
J

Jean-Guy Marcil

Montana DOJ Help Desk was telling us:
Montana DOJ Help Desk nous racontait que :
Thanks for the reply. I tried your suggestion, and it worked.
However, after reading in Help, I'm a little confused on the
difference between AutoOpen and Document_Open. It seems like either
should work in this particular circumstance. I did see in Help that
AutoOpen is global, whereas Document_Open is not. So when would
AutoOpen be used?

Not all Auto macros are deprecated; just the close, open and new. AFAICT,
there is no difference in execution between using an AutoNew and a
Document_New . It is just that the Document_New type are events and are
consistent with all Office applications. Most office application have events
related to the document or file (Excel has a lot and Word as only a few)

There are however auto macros that are truly global:
AutoExec and AutoExit for example. These two executes their code when Word
is launched, so the execute only once. They are typically used in global
templates located in the start-up folder so that they execute whenever Word
is opened/closed.

I am sure others can jump in with more details...
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Chuck was telling us:
Chuck nous racontait que :
How would you ensure displaying Montana DOJ's toolbar without adding
code to a document? Doesn't storing macros in documents mean that
they would travel with the documents if they were emailed to another
person/organisation? Other people/organisations who may be (quite
rightly) reluctant to open documents containing macros, fearing
malicious and/or uncontrollable code being set loose on their systems
without having first inspected the code to satisfy themselves that
it's not a problem. The specific code Montana DOJ is talking about
obviously isn't a problem, but as a general rule documents containing
macros are automatically suspect (at least anywhere I've ever
worked). Granted, the user has the option of "disabling macros" on
document open, but uneducated/careless/distracted users may click
"enable macros" instead, opening themselves to the possibility of
unexpected results.

Without macros you can't.
OTOH, if you use a template and a toolbar that contains only shortcuts to
styles and autotexts, then no macros are involved, and when documents based
on that template are created, the toolbar usually automatically displays, so
no macro needed.

With document-based toolbar you have less control.

Also, if your toolbar points to macros, than having macros to activate the
toolbar is not a problem since you already have macros in the document....

Or you need to purchase a signature that is generally trusted, such as
VeriSign... but that can be pricy...

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

Chuck

It seems to me that the preferable solution (for security reasons outlined in
my previous post) would be not to store the code in any document but in the
template on which it is based, which would require using the AutoOpen macro.
I am aware that some people may have ideological problems with AutoMacros in
general, but it would appear to be a useful alternative for people who don't
want to store code in documents. I've found that using AutoMacros (such as
AutoOpen, eg as Peter suggested in a previous post) in templates reliably
displays command bars even if they've been deselected.
 
J

Jean-Guy Marcil

Chuck was telling us:
Chuck nous racontait que :
It seems to me that the preferable solution (for security reasons
outlined in my previous post) would be not to store the code in any
document but in the template on which it is based, which would
require using the AutoOpen macro. I am aware that some people may

AutoOpen or ThisDocument.Document_Open in a template will have the exact
same effect.
AutoOpen is not required in any particular case, you can always use
Document_Open instead.
have ideological problems with AutoMacros in general, but it would

Some auto macros are still very useful, such as AutoExec or AutoExit.
appear to be a useful alternative for people who don't want to store
code in documents. I've found that using AutoMacros (such as
AutoOpen, eg as Peter suggested in a previous post) in templates
reliably displays command bars even if they've been deselected.

Any macros in a template (In an Auto macros or in the ThisDocument class
module, or anywhere else) will trigger the security warning or be
automatically disabled if the security is set at High. So using Auto macros
will not solve any problems, be it in a template or a document.
AutoOpen or Document_Open are not in any way part of the solution for the
toolbar question in this thread. I just took the opportunity to bring up the
fact that some Auto macros are deprecated. Tom could have used my suggestion
in an Auto macro set up with exactly the same result.
His problem, I think, was caused by his using
Toolbar("Name").Visible = False
in his AutoClose macro. Had he used the same code in a Document_Close macro,
he would have had the same problem.

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

Montana DOJ Help Desk

His problem, I think, was caused by his using
Toolbar("Name").Visible = False
in his AutoClose macro. Had he used the same code in a Document_Close macro,
he would have had the same problem.

I decided to test this theory. First, I added

Toolbar("Name").Visible = False

to my Document_Close routine. When I opened the document the Document_Open
routine displayed the toolbar. Next, I created AutoOpen and AutoClose
routines using exactly the same code as the Document_Open and Document_Close
routines, and then I deactivated Document_Open and Document_Close to avoid
conflicts with the auto macros. I tried AutoClose with, and without, the
above command, and in both cases the AutoOpen routine failed to display the
toolbar when the document was opened.

Next, I eliminated the AutoClose routine entirely, so that nothing was
running when the document closed. I also reduced AutoOpen to the following
(previously, I had one other command, but it was just setting a variable
value):

Private Sub AutoOpen()
CommandBars("Locates").Visible = True
End Sub

Still, AutoOpen did not display the toolbar when the document was opened.
Then it suddenly occurred to me that I had made AutoOpen a private routine.
So I removed "Private" from the above routine, and it worked!

I guess that makes sense because AutoOpen would have to be called from
outside of the module, and since the Private keyword makes a macro
unavailable from outside of the module, the routine simply wasn't getting
run.

This explains why I was certain that the routine worked when I first wrote
it over a year ago. At that time, I didn't know about make routines
private, so it was a public routine. When I discovered how to make private
routines, I went through my project and made a bunch of them private in
order to prevent them from appears in the Tools > Macro > Macros dialog box
in Word (to prevent users from routines that are not designed to be run
directly). That was a while back, and I obviously didn't think to test my
auto macros after making them private.

-- Tom

State of Montana
Department of Justice Help Desk
 
J

Jean-Guy Marcil

Montana DOJ Help Desk was telling us:
Montana DOJ Help Desk nous racontait que :
Still, AutoOpen did not display the toolbar when the document was
opened. Then it suddenly occurred to me that I had made AutoOpen a
private routine. So I removed "Private" from the above routine, and
it worked!

I guess that makes sense because AutoOpen would have to be called from
outside of the module, and since the Private keyword makes a macro
unavailable from outside of the module, the routine simply wasn't
getting run.

Thanks for the update.
It confirms what I thought, There is basically no difference between an Auto
macro and a Document event macro as far as execution goes.
Thanks for reminding us that this is true only if the Auto macro is public.
I never use Auto macros, so it did not occur to me to check if it was
private or not (even though it was staring me in the face in your original
post.... My bad!). This is why I was puzzled as to why you were having
problems and offered a possibility as the cause.

Yet another reason to use document events. These do not show up in the macro
list when you do ALT-F8 because they are by default private. Of course, if
you remove the Private keyword, they will be listed, but you have no reason
to remove the keyword.

Glad you sorted it out!

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

Montana DOJ Help Desk

It just goes to show the importance of testing, testing, testing. When I
made a bunch of the routines private, I tested extensively, and everything
seemed to be working. But I never thought to hide the toolbar and see if it
would be displayed when the document opened. During the development of this
project, I have always left the toolbar visible, as would be the case most
of the time with normal use. Because the toolbar was there every time I
opened the document, I thought the routines were working, when in fact
nothing at all was happening, and the toolbar was always displayed simply
because I never hid it.

So I was utterly flabbergasted the other night when I tested the routines
and discovered that they were not working. I couldn't figure out why they
once worked, and then suddenly they no longer worked, when I hadn't changed
them at all (I thought). It had just been long enough since I made them
private routines that I didn't remember having made that change.

I'm going to stick with the event macros because, as you pointed out, they
are private by default, they do the job, and do not appear in the list of
macros when you do ALT+F8.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
M

Montana DOJ Help Desk

I think that it's safe to say that on this one, we all failed to see the
forest through the trees! :)

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
"Peter" <peterguy -at- hotmail -dot- com> wrote in message
oh my, I can be so blind sometimes! :-$
 

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