G
Greg
Appologize for the mulitple subjects, but the question is complex (a
least to my simple mind).
With some earlier stumbling and help from Jezebel, I have managed to
"manually" control the display of a menu item on a the Tables toolbar.
The item is enabled if the selection is in a table and disabled (or
dimmed if not). This mimics the behaviour of the builtin Table menu
items.
To achieve this, I have a regular VBA modue with the following code:
Option Explicit
Public myDynamicMenu As myClass1
________________________________________
Sub Register_MyClass1()
Set myDynamicMenu = New myClass1
Set myDynamicMenu.appWord = Word.Application
End Sub
and a Class module with the following code:
Option Explicit
Private mMenuOption As Office.CommandBarControl
_________________________________________
Public WithEvents appWord As Word.Application
Private Sub Class_Initialize()
Set mMenuOption = CommandBars("Table").Controls("Table Data")
End Sub
_________________________________________
Private Sub appWord_WindowSelectionChange(ByVal oSel As Selection)
mMenuOption.Enabled = oSel.Information(wdWithInTable)
End Sub
To enable the dynamic menu display, I run: Sub Register_MyClass1
So far so good.
The problems:
1. Jezebel had asked me "Is there some reason that I declared
"WithEvents appWord As Word.Application" as Public?
My answer is that this is the only way I can make the thing work, so I
suspect that I am doing something wrong. Can anyone explain what I am
doing wrong or how I should initialize the Class module to keep the
declarations Private?
2. I keep Tools>Options>Save>Prompt to save Normal.Template checked.
However, as this class module is changing the menu display dynamically
every time I click in and out of a table, it is flagging Normal.dot and
each time I quit Word I am promted to save Normal.dot. I know how to
set Normal as Saved with code, but that would mask all changes. Is
there some way to avoid this promt when you know that there was a
specific change? If not, then I suppose while this has been an
interesting exercise, the nuisance of the pop-up is not worth the gain
of a dynamic menu.
3. As I stated earlier, this process is now initiated manually by
running Register_MyClass1. If I call Register_MyClass1 (or move the
code from Register_MyClass1) to an AutoExec macro the application
generates the following error immediately after Word opens and the
selection changes:
Run time error '424' Object required. Can't move the focus to the
control because it is invisible, not enabled, or of the type that does
not accept the focus."
The error occurs on this line:
mMenuOption.Enabled = oSel.Information(wdWithInTable)
of the Class module code.
However, if I stet out the call from the AutoExec macro, let Word fully
open, then go back and unstet the call and step through the AutoExec
macro, then things work properly again.
Is this a timing issue or is there some way to wait until after Word is
fully opened before executing the call?
Thanks for any answers you can provide.
least to my simple mind).
With some earlier stumbling and help from Jezebel, I have managed to
"manually" control the display of a menu item on a the Tables toolbar.
The item is enabled if the selection is in a table and disabled (or
dimmed if not). This mimics the behaviour of the builtin Table menu
items.
To achieve this, I have a regular VBA modue with the following code:
Option Explicit
Public myDynamicMenu As myClass1
________________________________________
Sub Register_MyClass1()
Set myDynamicMenu = New myClass1
Set myDynamicMenu.appWord = Word.Application
End Sub
and a Class module with the following code:
Option Explicit
Private mMenuOption As Office.CommandBarControl
_________________________________________
Public WithEvents appWord As Word.Application
Private Sub Class_Initialize()
Set mMenuOption = CommandBars("Table").Controls("Table Data")
End Sub
_________________________________________
Private Sub appWord_WindowSelectionChange(ByVal oSel As Selection)
mMenuOption.Enabled = oSel.Information(wdWithInTable)
End Sub
To enable the dynamic menu display, I run: Sub Register_MyClass1
So far so good.
The problems:
1. Jezebel had asked me "Is there some reason that I declared
"WithEvents appWord As Word.Application" as Public?
My answer is that this is the only way I can make the thing work, so I
suspect that I am doing something wrong. Can anyone explain what I am
doing wrong or how I should initialize the Class module to keep the
declarations Private?
2. I keep Tools>Options>Save>Prompt to save Normal.Template checked.
However, as this class module is changing the menu display dynamically
every time I click in and out of a table, it is flagging Normal.dot and
each time I quit Word I am promted to save Normal.dot. I know how to
set Normal as Saved with code, but that would mask all changes. Is
there some way to avoid this promt when you know that there was a
specific change? If not, then I suppose while this has been an
interesting exercise, the nuisance of the pop-up is not worth the gain
of a dynamic menu.
3. As I stated earlier, this process is now initiated manually by
running Register_MyClass1. If I call Register_MyClass1 (or move the
code from Register_MyClass1) to an AutoExec macro the application
generates the following error immediately after Word opens and the
selection changes:
Run time error '424' Object required. Can't move the focus to the
control because it is invisible, not enabled, or of the type that does
not accept the focus."
The error occurs on this line:
mMenuOption.Enabled = oSel.Information(wdWithInTable)
of the Class module code.
However, if I stet out the call from the AutoExec macro, let Word fully
open, then go back and unstet the call and step through the AutoExec
macro, then things work properly again.
Is this a timing issue or is there some way to wait until after Word is
fully opened before executing the call?
Thanks for any answers you can provide.