
Getting word toolbar to reflect context of cursor position
Hi Chris,
Quote:
> Can you think of a way to get an event like cursor position to invoke a
function?
The only way this be done is by raising the eventhandler up the application
level.
Look it up in the online VBA help, topic: Application Object
or look in
http://www.mvps.org/word/FAQs/MacrosVBA/AppClassEvents.htm
If you can't manage to raise the eventhandler up to this level, repost but
start up
a new thread/
Under the newly created class module, following event will suit yr purpose:
WindowSelectionChange
Note: because the scope of this eventhandler goes beyond the scope of
yr target template/addin/document, you'll have to utilize a strict
error handling and a correct document assignment in code,
like examplified below.
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
On Error GoTo DoNothing
'are we speaking abt the correct document being active?
If ActiveDocument = Documents("TheRight.Doc") Then
Dim cbo As Object
'document is loaded, so is the commandbar
Set cbo = CommandBars("MyBar").Controls("MyCombo")
On Error Resume Next
'note: line continuation mark in following code line
cbo.ListIndex = _
GetListIndex(Selection.Range.Text, cbo)
Set cbo = Nothing
End If
ExitHere:
Exit Sub
Foutje:
Resume ExitHere
End Sub
Krgrds,
Perry
Quote:
> Hi Chris,
> > (change the .ListIndex to be the appropriate one)?
> There you go, affirmative.
> 'this defaults the commandbar combo to the
> 'Selection.Range text in your document.
> cbo.ListIndex = GetListIndex(Selection.Range.Text, cbo)
> The function to go with it (just as an example):
> Function GetListIndex(SelectionText As String, _
> MyCombo As CommandBarComboBox) As Long
> For x = 1 To MyCombo.ListCount
> If SelectionText = MyCombo.List(x) Then
> GetListIndex = x
> Exit Function
> End If
> Next
> End Function
> Krgrds,
> Perry
> > Hello,
> > I cannot find a way to do this and doubt that it can be
> > done but I thought I'd see if anyone knows of way.
> > I have a word toolbar with some dropdowns. When the user
> > has their cursor in an area of text that corresponds to
> > something that you can choose in a dropdown I want to have
> > that dropdown automatically detect that the cursor is in
> > an area of one of its items and then select that item
> > (change the .ListIndex to be the appropriate one)?
> > thanks,
> > Chris