How do I get an event when a menu is about to be displayed

D

David Thielen

Hi;

I am writing a word add-on in C# + VSTO but I think the API stays the
same.

First off. CommandBars.OnUpdate is called a lot so it isn't a good
solution for this problem.

My problem is I have 3 menu items that are enabled/disabled depending
on the caret position in the document. I don't want to go off the
selection change event because that gets fired a lot.

Is there some event that is fired just before a menu is displayed?

thanks - dave
 
D

Doug Robbins - Word MVP

To get a list of the available events for the version of Word that you are
using look up Application Events in the VBA Help File.

You may also want to take a look at the MSDN Article "Understanding the Word
Object Model from a
..NET Developer's Perspective" at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/WordObject.asp


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Cindy M -WordMVP-

Hi David,

As I replied to a duplicate of this question, posted a few days ago in
an office.developer group: I think your only chance is to work off the
ActionControl property of the CommandBars collection. Have you looked
into that?
I am writing a word add-on in C# + VSTO but I think the API stays the
same.

First off. CommandBars.OnUpdate is called a lot so it isn't a good
solution for this problem.

My problem is I have 3 menu items that are enabled/disabled depending
on the caret position in the document. I don't want to go off the
selection change event because that gets fired a lot.

Is there some event that is fired just before a menu is displayed?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
D

David Thielen

Hello;

As far as I can tell, ActionControl.OnAction is passed a string which
is a word macro. But I need to be called inside my C# code as I have
to run some code to determine which menu items I should disable.

thanks - dave
 
D

David Thielen

Hello again;

Is there a way for a C# method to be viewed as a Word macro? I didn't
see anything with a fast look but that does seem to make sense.

thanks - dave
 
C

Cindy M -WordMVP-

Hi David,
Is there a way for a C# method to be viewed as a Word macro? I didn't
see anything with a fast look but that does seem to make sense.
You'd have to use a Callback. Note that I've never done this, just seen
it discussed. Here's a set of instructions I picked up during VSTO beta
(written for Excel, but it should work the same for Word):

--
Here's an example for you that illustrates with the OnKey...

1. Create a new workbook and put the following code in a module:

Dim managedObject As Object

Public Sub RegisterCallback(callback As Object)
Set managedObject = callback
Application.OnKey "^m", "DoManagedCallBack"
End Sub

Public Sub DoManagedCallBack()
managedObject.HandleCtrlM
End Sub

2. Then, create a new Excel project based on the existing workbook you
created.

3. Add the following code to the project:

Private Sub ThisWorkbook_Open() Handles ThisWorkbook.Open
ThisApplication.Run("RegisterCallback", Me)
End Sub

Public Sub HandleCtrlM()
MsgBox("HandleCtrlM")
End Sub

4. Press F5 to run; when you press Ctrl+m in Excel, the
DoManagedCallBack
routine in the assembly is run.

So, what's happening here is that a) you call a macro in the workbook
to
pass it a reference to the assembly's class, b) the macro stores the
reference to the object, c) you set up OnKey to call a macro in the
workbook, and d) when the "OnKey" routine fires, it will use the stored
reference to the object to call a public method (HandleCtrlM in this
example) in the assembly code. Of course, for this to work, the VBA
security settings must allow the VBA code in the workbook to run.
--

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
D

David Thielen

Hi;

When I change the text in the word document using the range, is there
a way to also set the text that will appear as the Undo item in the
Edit menu?

thanks - dave
 
P

Peter Hewett

Hi David Thielen

You can only do 2 things with undo and that's undo an action (or n actions) or clear the
undo buffer. You have no control over what Word displays in it's Undo/Redo controls.

HTH + Cheers - Peter
 
D

David Thielen

Yuck - not the answer I wanted.

thanks - dave

Hi David Thielen

You can only do 2 things with undo and that's undo an action (or n actions) or clear the
undo buffer. You have no control over what Word displays in it's Undo/Redo controls.

HTH + Cheers - Peter
 
D

David Thielen

Hi;

When the caret is on a field, the selection is for the text char just
before the field. How can I find out if a selection is on a field?

thanks - dave
 
P

Peter Hewett

Hi David Thielen

Try the following code it will tell you if the selection is within, contains or is
immediately adjacent (cause the field shading to show) to a field:

Public Function SelectionContainsAField() As Boolean
Dim rngAdjacent As Word.Range
Dim rngParagraph As Word.Range
Dim fldItem As Word.Field

' If the insertion point is in a field the field count
' is zero. This code allows for that problem.

' No more to do if the selected range contains a field
If Selection.Fields.Count > 0 Then
SelectionContainsAField = True
Exit Function
End If

' Copy the current selection into range objects
' so that we don't change the selection
Set rngParagraph = Selection.Range
Set rngAdjacent = Selection.Range

' Extend range to include current paragraph, if Selection is
' in a field the range is expanded to include that field
rngParagraph.Expand wdParagraph

For Each fldItem In rngParagraph.Fields

' If the selection is an insertion point adjacent to
' the field, then that also constitutes a match.
If Selection.Type = wdSelectionIP Then
If rngAdjacent.Start = fldItem.Code.Start - 1 Then
SelectionContainsAField = True
Exit For
End If
End If

' Make sure IP is really in a field and it's not just
' the expanded paragraph that picked up a field
If Selection.InRange(fldItem.Result) Then
SelectionContainsAField = True
Exit For
End If
Next
End Function

HTH + Cheers - Peter
 
D

David Thielen

this is great - thanks - dave

Hi David Thielen

Try the following code it will tell you if the selection is within, contains or is
immediately adjacent (cause the field shading to show) to a field:

Public Function SelectionContainsAField() As Boolean
Dim rngAdjacent As Word.Range
Dim rngParagraph As Word.Range
Dim fldItem As Word.Field

' If the insertion point is in a field the field count
' is zero. This code allows for that problem.

' No more to do if the selected range contains a field
If Selection.Fields.Count > 0 Then
SelectionContainsAField = True
Exit Function
End If

' Copy the current selection into range objects
' so that we don't change the selection
Set rngParagraph = Selection.Range
Set rngAdjacent = Selection.Range

' Extend range to include current paragraph, if Selection is
' in a field the range is expanded to include that field
rngParagraph.Expand wdParagraph

For Each fldItem In rngParagraph.Fields

' If the selection is an insertion point adjacent to
' the field, then that also constitutes a match.
If Selection.Type = wdSelectionIP Then
If rngAdjacent.Start = fldItem.Code.Start - 1 Then
SelectionContainsAField = True
Exit For
End If
End If

' Make sure IP is really in a field and it's not just
' the expanded paragraph that picked up a field
If Selection.InRange(fldItem.Result) Then
SelectionContainsAField = True
Exit For
End If
Next
End Function

HTH + Cheers - Peter
 
D

David Thielen

Hi;

To bring up the help for my add-in, I need the Control (VSTO/C#) that
is the main Word window.

Any idea how to get this?

thanks - dave
 
D

David Thielen

Hi;

I am trying to strongly name my Word add-in (VSTO/C#). However, I need
to display a web page in it and therefore need to use (and reference)
AxInterop.SHDocVw.

When I go to compile, the assembly generation fails as
AxInterop.SHDocVw is not strongly signed.

What can I do?

thanks - dave
 
D

David Thielen

Hi;

If I want to call Help.ShowHelp() and I do not presently have a form
open, how should I do it?

I am presently passing it "new Control()" as it's parent.

??? - dave
 
D

David Thielen

Hi;

I need to install my .DOT file under Documents and Settings\user\...
Any idea how to set this in the Windows Installer?

thanks - dave
 
J

Jay Freedman

David Thielen said:
Hi;

I need to install my .DOT file under Documents and Settings\user\...
Any idea how to set this in the Windows Installer?

thanks - dave

Hi Dave,

See
http://msdn.microsoft.com/library/d...ry/en-us/msi/setup/appdatafolder_property.asp.

The predefined AppDataFolder property contains the path of the current
user's Documents and Settings\user\Application Data folder, and the
folder for installing templates is Microsoft\Templates under that. So
set the Destination of the component of your template to the
expression

[AppDataFolder]Microsoft\Templates
 
D

David Thielen

Thank you - that's exactly what I needed.

David Thielen said:
Hi;

I need to install my .DOT file under Documents and Settings\user\...
Any idea how to set this in the Windows Installer?

thanks - dave

Hi Dave,

See
http://msdn.microsoft.com/library/d...ry/en-us/msi/setup/appdatafolder_property.asp.

The predefined AppDataFolder property contains the path of the current
user's Documents and Settings\user\Application Data folder, and the
folder for installing templates is Microsoft\Templates under that. So
set the Destination of the component of your template to the
expression

[AppDataFolder]Microsoft\Templates
 
J

Jonathan West

David Thielen said:
Thank you - that's exactly what I needed.

I would counsel caution. The [AppDataFolder]Microsoft\Templates folder will
only be the user templates folder if the user hasn't gone into Tools,
Options, File Locations and changed it. This cannot be guaranteed.

If you want to use Windows installer, you might prefer the approach of
having installer park the templates in some convenient location, and then
execute a VBScript file which opens an instance of Word, reads the user
templates folder from the relevant property of the Word object model, and
copies the templates to that location, wherever it might be.
 
W

Word Heretic

G'day David Thielen <[email protected]>,

I wouldn't be using the view, I'd be using

WordApp.Documents

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


David Thielen reckoned:
 

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