Can I override Word commands in a plugin?

A

Alex

For example, I can override the CTRL-Z and CTRL-Y commands with EditUndo() and EditRedo() macros.
Can I do the same in an add-in code without a macro?
Alternatively, can I programmatically create a macro in a user document?

thank you.


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi Alex,

Since the Word will try to find the EditUndo macro, so we need to add the
macro by coding.
Here is a simple sample for your reference.

object oMissing = System.Reflection.Missing.Value;
CommandBar cb;
CommandBarButton cbb;
public void OnStartupComplete(ref System.Array custom)
{
cb = wdApp.CommandBars.Add("TestToolBar",oMissing,oMissing,oMissing);
cbb =
(CommandBarButton)cb.Controls.Add(MsoControlType.msoControlButton,oMissing,o
Missing,oMissing,oMissing);
cbb.Caption = "TestButton";
cbb.Click+=new _CommandBarButtonEvents_ClickEventHandler(cbb_Click);
}

private Word.Application wdApp=null;

private void cbb_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
Debug.WriteLine("Button Clicked"+System.DateTime.Now.ToString());
try
{
VBE.VBComponent vbc
=wdApp.ActiveDocument.VBProject.VBComponents.Add(VBE.vbext_ComponentType.vbe
xt_ct_StdModule);
vbc.Name = "Hello";
vbc.CodeModule.InsertLines(1,"Sub EditUndo()\nMsgBox \"Undo Hooked\"\nEnd
Sub");
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
Debug.WriteLine("Button Clicked"+System.DateTime.Now.ToString());
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hello Peter,

"Peter Huang" said:
Since the Word will try to find the EditUndo macro, so we need to add the
macro by coding.
Here is a simple sample for your reference.

<snip>

Thank you for the reply.

So the only way to do it is to add a macro (such as "EditUndo") programmatically?

Is it possible for the macro to call C# code in my add-in?

Can you point me to a link with more info about VBE?

Thank you.

Best wishes,
Alex.
 
C

Cindy M -WordMVP-

Hi Alex,
This is the Office '97 version and the page says:
"Archived content. No warranty is made as to technical accuracy."

Doesn't inspire confidence that it will work in the latest versions of Word... :-(
Actually, the VBE hasn't changed substantially in all that time :)

As to your other question, you'd still need code in the document project. Word can't
link directly to an Addin, the way it can to its own, local projects. In order to get
it to call your code from a keyboard action within the UI you require a "callback"
procedure in the Word environment, linking the keycode to your addin. Ditto for the
built-in commands, I should think.

Now, as to your original question, controlling Ctrl+Z and Ctrl+Y, you could turn
these off completely by accessing the KeyBindings collection and disabling them.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
A

Alex

Hello Cindy,

Cindy M -WordMVP- said:
Actually, the VBE hasn't changed substantially in all that time :)
OK.

As to your other question, you'd still need code in the document project. Word can't
link directly to an Addin, the way it can to its own, local projects. In order to get
it to call your code from a keyboard action within the UI you require a "callback"
procedure in the Word environment, linking the keycode to your addin. Ditto for the
built-in commands, I should think.

Can you give an example of a "callback"?
Now, as to your original question, controlling Ctrl+Z and Ctrl+Y, you could turn
these off completely by accessing the KeyBindings collection and disabling them.

Actually, I was thinking about something like this:
http://groups.google.ca/group/microsoft.public.word.word97vba/msg/c5f120a7817f3dbb

Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi Alex,

I agree with Cindy's suggestion.

Also for callback from VBA to Addin,h ere goes the code snippet.

Modify the attribute of the Connect ,add the Hello test funtion, so that
the com client, i.e. the VBA can see the function by using COM interface.
[GuidAttribute("E661AF83-1B16--4983-A64C-5E5FEDC0C4FC"),
ProgId("WordAddin.Connect"),Cl-assInterface(ClassInterfaceTyp-e.AutoDual),Co
mV
isible(true)]
public class Connect : Object, Extensibility.IDTExtensibility-2


public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
((Microsoft.Office.Core.COMAdd-In)addInInst).Object
= this; //this is necessary.
}
public void Hello(string str)
{
System.Windows.Forms.MessageBox.Show(str);
}


Now we can call the Hello in the word macro.
e.g.
Sub Test()
Dim o As Object
Set o = Application.COMAddIns.Item("WordAddin.Connect").Object
o.Hello "df"
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cindy M -WordMVP-

Hi Alex,
Ah, yes, interesting...

You would need to put code directly into the document's project (or its template's).

A callback isn't something I've gotten around to using, myself, but here's an
example of what is meant, from a beta group I was in:

<<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 Jun 8 2004)
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 :)
 

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