Re: Enable/Disable Toolbar button

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Unfortunately, no. That one is disabled if the document is protected, so it
stays disabled even if I 're-purpose' it. And that was what got me started
down this track anyway. If the built-in Restart Numbering and Continue
Numbering worked in protected documents then I'd just use them instead of
trying to develop my own. Catch-22 really.

And then I ran into another problem that made me bag the whole works.
Changing the state of a toolbar button effectively makes the global template
'dirty' so the user gets prompted to save the template on exiting Word. I
tried forcing a save of the global template after changing the button state
but that unloaded the template - and also automagically made it so the mouse
didn't work for changing the selection; I could only use the arrow keys, and
then because the template was no longer loaded, the WindowSelectionChange
event didn't get captured. GAHHH!!!

I just went back to the 'ambulance at the bottom' technique. Now if the user
clicks on one of the buttons when the cursor isn't in a numbered paragraph
(or the selection type is wdSelectionIP), it pops a message box. I figure it
will only take a couple of times for the users to work out how to use the
functionality. Not ideal I know, but the deadline on the project is 4pm
today... Maybe when I have a bit more time I'll see if I can get it working
properly. I'm sure there'll be maintenance and upgrade releases at some
point, and I can slip the 'proper' functionality in under one of them.

Anyway, thanks again for all your help. I really do appreciate it. And even
if I can't use this new knowledge on this project, I'm sure it will come in
handy at some other point.
--
Cheers!
Gordon


"Shauna Kelly" wrote:

Wouldn't the built-in Restart Numbering button do?

Hope this helps.

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


"NZ VBA Developer" <gordon(dot)bentleymix(at)gmail(dot)com> wrote in message
news:08130CF3-8A2C-489B-9D48-B5BC66FD8BC1@xxxxxxxxxxxxxxxx
No joy, Shauna. As I suspected, there's no 'doner' button that I can use
as
my starting point - no button that's enabled only when the selection point
is
in a numbered paragraph AND the document is protected. Looks like I'm just
going to have to go with the WindowSelectChange event code I wrote
previously. At least this works some of the time.

However, your efforts weren't totally wasted. I have another button - the
one for rerunning the macros for creating the doc - that I want to be
enabled
only when the attached template is one of the templates that I developed.
The
information you provided has given me a lead on how to make this work.

Thanks again for all your help.
--
Cheers!
The (Plastik) Kiwi Koder


"Shauna Kelly" wrote:

Hi

Sorry, I kind of said one thing and meant something very different.

The WindowSelectionChange doesn't fire when the user types letters or
uses
any function keys or shortcut keys. For example, if the user applies a
style
to the selection, the WindowSelectionChange event won't fire.

Since you want to detect when the insertion point is in a numbered or
bulleted paragraph, then the WindowSelectionChange won't do it for you.
If
style Heading 1 is numbered, the user can apply that style, and thus the
paragraph at the insertion point will be numbered, without the event
firing.
The same applies to applying bulleted styles and even (gasp!) using the
bullets or numbering buttons on the toolbar. And, the same applies in
reverse, if the user applies an un-bulleted style to a paragraph that has
bullets.

Hope this helps.

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


"NZ VBA Developer" <gordon(dot)bentleymix(at)gmail(dot)com> wrote in
message
news:2DC04275-454C-46E1-A8ED-ACA3EA1B0604@xxxxxxxxxxxxxxxx
Shauna,

I'm just re-reading your post to make sure I understand. Are you saying
that
the WindowSelectionChange event isn't triggered when the cursor is
repositioned using the arrow keys? Or clicks through a cross-reference,
such
as a TOC entry? Cuz it seems to be working OK for me - well at least
the
arrow keys are. I haven't tried using the Document Map yet to see what
happens, but since my 'target audience' isn't especially Word-savvy, I
may
just leave it alone; I'm not sure anyone at the client site even knows
what
the Document Map is.

The only time I noticed the buttons not behaving as expected was when I
first applied a numbered style to a selection - altho they may have
been
behaving oddly when I was typing as well. And even then I'm not sure;
I'll
have to experiment a bit more when I have some time later on.

In any case, I do appreciate the information, and if it turns out that
I
really do need it, I'm sure I'll appreciate it even more!

--
Cheers!
Gordon


"Shauna Kelly" wrote:

Hi Gordon

The WindowSelectionChange event won't fire when the user negotiates
with
the
cursor instead of the mouse, and it doesn't fire if the user clicks
the
Document Map to move around. To get around that, you can use the
built-in
functionality that Word has in its various buttons.

Find a built-in button that has the functionality you want. That is,
find
a
built-in button that turns on and off when you want yours to turn on
and
off. (For example, if you want a button that is only enabled when the
insertion point is in a table, then choose one of the buttons on the
Table
menu that operates as you want.)

Then *copy* the button you found to your own toolbar. You can then
change
the icon and the displayed text to suit yourself. If you need more
than
one
button to be enabled and unenabled at the same time, but do different
things, then copy the *same* button (and change the icon and displayed
text
to suit yourself).

Use the Immediate Pane to determine the ID of the button (let's say
it's
296). Using the Immediate Pane, give your button(s) a .Tag (something
meaningful to you that identifies it as belonging to your project,
let's
say
"Gordon"). You may have several buttons with the same .ID and .Tag
combination. Give each button a separate .Parameter, which identifies
each
one uniquely (let's say "Gordon_RestartNumbering" and
"Gordon_ContinueNumbering"). In my example here I used the useless
names
of
"Param1" and "Param 2".

So, in my example document I now have a custom toolbar named "Custom
1"
with
two buttons. They both have the same .ID of 296. They both have a .Tag
of
"Gordon". One has .Parameter "Param1" and the other is "Param2". And
if I
play with Word, they turn on and off as I need them to.

Now use the following code:

1. In an ordinary Module:

Option Explicit

Public gMyButton As CMyButtons



2. In the ThisDocument class:

Option Explicit

Private Sub Document_New()
Set gMyButton = New CMyButtons
End Sub

Private Sub Document_Open()
Set gMyButton = New CMyButtons
End Sub



3. In a Class module named CMyButtons

Option Explicit

'A variable to hold a reference to any button with your combination
'of ID and Tag
Private WithEvents mMyButton As Office.CommandBarButton

Private Sub Class_Initialize()
'Set mMyButton to refer to a button with your combination of ID
'and Tag. Even though you may have many buttons with the same ID
'and Tag combination, you only need to 'hook' one of them here.

'This way is fast
Set mMyButton = CommandBars("Custom 1").Controls(1)

'OR....
'This way is safe
Set mMyButton = CommandBars.FindControl(Type:=msoControlButton,
ID:=296,
Tag:="Gordon")

End Sub

Private Sub mMyButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)

'This fires when the user clicks one of your buttons with your
combination
'of ID and Tag

If Ctrl.Tag = "Gordon" And Ctrl.ID = 296 Then
'Strictly speaking you don't need this whole If ... .Tag *and*
.ID
thing here
'but I find it enormously helpful documentation!

Select Case Ctrl.Parameter
Case "Param1"
MsgBox "You clicked my Param1 button"
'Run the appropriate code here

Case "Param2"
MsgBox "You clicked my Param2 button"
'Run the appropriate code here
End Select

'prevent Word from doing the default action
'for the button
CancelDefault = True

End If

End Sub



Create a new document from your template and watch the magic happen!

For the record, any .OnAction value of the button(s) is ignored when
you
hook buttons like this.

Now, go buy a copy of Professional Excel Development by Bullen, Bovey
and
Green where you'll read a much better description of this than I have
given.
Ignore the early chapters about Excel, if you want to. The book is
worth
it
for the latter chapters and it applies equally well to Word as to
Excel.

Hope this helps.

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

(England v the Springboks. Who would have thought?)




"NZ VBA Developer" <gordon(dot)bentleymix(at)gmail(dot)com> wrote in
message
news:E11A9728-C8F3-4729-BC7B-BF2DF8766A6C@xxxxxxxxxxxxxxxx
Hmm... Not quite Jay...

Naming the macros as suggested does make it so they run from the
'native'
toolbar button or the right-click menu, but it still doesn't get
around
the
original problem. The native toolbar buttons and the right-click
menu
items
are still disabled when the document is protected.

What I need is a combination of the behaviour provided by the
buttons
for
the native functionality and the behaviour of buttons with macros
assigned
to
them: enabled even though the document is protected but only when
the
context
is correct. In fact, I'd be happy to use the native functionality
rather
than
my macro since all I'm trying to do is to give the users the ability
to
restart/continue numbering, and the native functionality does this
just
fine
- just not when the doc is protected. All my macro does is change an
argument
for the ApplyListTemplate method as follows:

Sub RestartNumbering()
Dim LT As ListTemplate
Set LT = Selection.Style.ListTemplate
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=LT,
ContinuePreviousList:=False
.



Relevant Pages

  • Re: Enable/Disable Toolbar button
    ... On dirtying the template: ... and I can slip the 'proper' functionality in under one of them. ... You may have several buttons with the same .ID and .Tag ... Private Sub Document_Open ...
    (microsoft.public.word.vba.general)
  • Re: Enable/Disable Toolbar button
    ... Find a built-in button that has the functionality you want. ... Then *copy* the button you found to your own toolbar. ... Private Sub Document_Open ... Naming the macros as suggested does make it so they run from the 'native' ...
    (microsoft.public.word.vba.general)
  • Re: Enable/Disable Toolbar button
    ... Find a built-in button that has the functionality you want. ... Then *copy* the button you found to your own toolbar. ... Private Sub Document_Open ... Naming the macros as suggested does make it so they run from the 'native' ...
    (microsoft.public.word.vba.general)
  • Re: Enable/Disable Toolbar button
    ... Find a built-in button that has the functionality you want. ... Then *copy* the button you found to your own toolbar. ... Private Sub Document_Open ... Naming the macros as suggested does make it so they run from the 'native' ...
    (microsoft.public.word.vba.general)
  • Re: Inserting a graphic at a bookmark with a Userform
    ... Create a dummy AutoText entry that contains a space character, give it a valid name, and use that name in the "nologo" case. ... Dim ATname As String ... Private Sub UserForm_Initialize ... header of the template. ...
    (microsoft.public.word.vba.general)