Word does it, so how can I do it?

I want to enable/disable a toolbar button depending on cursor
location/selection. For example, if I put the 'Restart Numbering' and
'Continue Numbering' buttons on the 'Formatting' toolbar, Word "knows" to
enable these buttons _only_ if the current selection is numbered. Any ideas
on how to replicate this functionality for the buttons on my own custom
toolbar so the macro assigned to the button can be run only in the correct
context?

(In fact, the 'Restart/Continue Numbering' functionality is the
functionality that I want to replicate, but I can't just add the native Word
buttons to my custom toolbar because this functionality doesn't work with
protected documents. So if somebody has a clever idea for getting around
this...)

--
Cheers!
The Kiwi Koder
Go the (~cough-splutter-gag~) 'Boks...

Re: Enable/Disable Toolbar button by Jay

Jay
Sun Oct 14 19:15:08 PDT 2007

If you name your macros RestartNumbering and ContinueNumbering, then
the built-in buttons (with their automatic enable/disable behavior)
will run the macros instead of the built-in commands. The macros will
also intercept the commands on the context (right-click) menu, which
appear only when the appropriate text is clicked.
http://www.word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm

On Sun, 14 Oct 2007 18:33:00 -0700, NZ VBA Developer
<gordon(dot)bentleymix(at)gmail(dot)com> wrote:

>Word does it, so how can I do it?
>
>I want to enable/disable a toolbar button depending on cursor
>location/selection. For example, if I put the 'Restart Numbering' and
>'Continue Numbering' buttons on the 'Formatting' toolbar, Word "knows" to
>enable these buttons _only_ if the current selection is numbered. Any ideas
>on how to replicate this functionality for the buttons on my own custom
>toolbar so the macro assigned to the button can be run only in the correct
>context?
>
>(In fact, the 'Restart/Continue Numbering' functionality is the
>functionality that I want to replicate, but I can't just add the native Word
>buttons to my custom toolbar because this functionality doesn't work with
>protected documents. So if somebody has a clever idea for getting around
>this...)

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Re: Enable/Disable Toolbar button by gordon(dot)bentleymix(at)gmail(dot)com>

gordon(dot)bentleymix(at)gmail(dot)com>
Sun Oct 14 19:48:01 PDT 2007

Doh! Why didn't I think of that? That's exactly what I do to get around that
bloody Task Pane popping up whenever the 'File | New' button is clicked. I
have a simple one-liner - named 'FileNew' - that just displays the 'New'
dialog box.

However, I wonder if the same 'disable in protected documents' behaviour
will still apply... I'll give it a go and post the results one way or another.

--
Cheers!
The Kiwi Koder


"Jay Freedman" wrote:

> If you name your macros RestartNumbering and ContinueNumbering, then
> the built-in buttons (with their automatic enable/disable behavior)
> will run the macros instead of the built-in commands. The macros will
> also intercept the commands on the context (right-click) menu, which
> appear only when the appropriate text is clicked.
> http://www.word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm
>
> On Sun, 14 Oct 2007 18:33:00 -0700, NZ VBA Developer
> <gordon(dot)bentleymix(at)gmail(dot)com> wrote:
>
> >Word does it, so how can I do it?
> >
> >I want to enable/disable a toolbar button depending on cursor
> >location/selection. For example, if I put the 'Restart Numbering' and
> >'Continue Numbering' buttons on the 'Formatting' toolbar, Word "knows" to
> >enable these buttons _only_ if the current selection is numbered. Any ideas
> >on how to replicate this functionality for the buttons on my own custom
> >toolbar so the macro assigned to the button can be run only in the correct
> >context?
> >
> >(In fact, the 'Restart/Continue Numbering' functionality is the
> >functionality that I want to replicate, but I can't just add the native Word
> >buttons to my custom toolbar because this functionality doesn't work with
> >protected documents. So if somebody has a clever idea for getting around
> >this...)
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>

Re: Enable/Disable Toolbar button by gordon(dot)bentleymix(at)gmail(dot)com>

gordon(dot)bentleymix(at)gmail(dot)com>
Sun Oct 14 20:31:01 PDT 2007

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
End Sub

Sub ContinueNumbering()
Dim LT As ListTemplate
Set LT = Selection.Style.ListTemplate
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=LT,
ContinuePreviousList:=True
End Sub

Is there a way to capture/monitor the 'selection change' event using VBA?
Seems to me that this is what Word is doing, and I've seen COM add-ins do the
same...
--
Cheers!
The Kiwi Koder


"Jay Freedman" wrote:

> If you name your macros RestartNumbering and ContinueNumbering, then
> the built-in buttons (with their automatic enable/disable behavior)
> will run the macros instead of the built-in commands. The macros will
> also intercept the commands on the context (right-click) menu, which
> appear only when the appropriate text is clicked.
> http://www.word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm
>
> On Sun, 14 Oct 2007 18:33:00 -0700, NZ VBA Developer
> <gordon(dot)bentleymix(at)gmail(dot)com> wrote:
>
> >Word does it, so how can I do it?
> >
> >I want to enable/disable a toolbar button depending on cursor
> >location/selection. For example, if I put the 'Restart Numbering' and
> >'Continue Numbering' buttons on the 'Formatting' toolbar, Word "knows" to
> >enable these buttons _only_ if the current selection is numbered. Any ideas
> >on how to replicate this functionality for the buttons on my own custom
> >toolbar so the macro assigned to the button can be run only in the correct
> >context?
> >
> >(In fact, the 'Restart/Continue Numbering' functionality is the
> >functionality that I want to replicate, but I can't just add the native Word
> >buttons to my custom toolbar because this functionality doesn't work with
> >protected documents. So if somebody has a clever idea for getting around
> >this...)
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>

Re: Enable/Disable Toolbar button by Jay

Jay
Sun Oct 14 20:57:28 PDT 2007

Yeah, I was afraid that might happen. Ratz.

There is a WindowSelectionChange event that you can monitor -- see
http://www.word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm for the
necessary setup. If you can figure out the proper conditions to put in
an IF statement inside the event handler, you can enable/disable a
pair of custom buttons.

On Sun, 14 Oct 2007 20:31:01 -0700, NZ VBA Developer
<gordon(dot)bentleymix(at)gmail(dot)com> wrote:

>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
>End Sub
>
>Sub ContinueNumbering()
> Dim LT As ListTemplate
> Set LT = Selection.Style.ListTemplate
> Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=LT,
>ContinuePreviousList:=True
>End Sub
>
>Is there a way to capture/monitor the 'selection change' event using VBA?
>Seems to me that this is what Word is doing, and I've seen COM add-ins do the
>same...

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Re: Enable/Disable Toolbar button by gordon(dot)bentleymix(at)gmail(dot)com>

gordon(dot)bentleymix(at)gmail(dot)com>
Mon Oct 15 03:22:01 PDT 2007

Jay,

Thanks for the tip. I've just about got it sussed now. Here's the code I'm
using. It seems to work OK and I haven't noticed any major hit in performance.

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
On Error Resume Next
If Sel.Type = wdSelectionIP Then
Dim SelStyleName As String
SelStyleName = Sel.Style.NameLocal
Select Case SelStyleName
Case "Heading 1", "Heading 2", "Heading 3", "Heading 4", "List
Number", "List Number 1", "Table List Number", "Table List Number 2"
With CommandBars("Toolkit")
.Controls(6).Enabled = True
.Controls(7).Enabled = True
End With
Case Else
With CommandBars("Toolkit")
.Controls(6).Enabled = False
.Controls(7).Enabled = False
End With
End Select
End If
End Sub


My first time writing a Class Module. It's got me thinking about other
possibilities...
--
Cheers!
The Kiwi Koder


"Jay Freedman" wrote:

> Yeah, I was afraid that might happen. Ratz.
>
> There is a WindowSelectionChange event that you can monitor -- see
> http://www.word.mvps.org/FAQs/MacrosVBA/ApplicationEvents.htm for the
> necessary setup. If you can figure out the proper conditions to put in
> an IF statement inside the event handler, you can enable/disable a
> pair of custom buttons.
>
> On Sun, 14 Oct 2007 20:31:01 -0700, NZ VBA Developer
> <gordon(dot)bentleymix(at)gmail(dot)com> wrote:
>
> >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 t