Programmatically create a macro?

A

Alex

Is it possible to programmatically create Word VBA macros from a C# add-in?
That is, given a piece of text, to insert it into a document or template as a macro?

Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

Here is some code line for your reference.

object oMissing = System.Reflection.Missing.Value;
CommandBar cb;
CommandBarButton cbb;
public void OnStartupComplete(ref System.Array custom)
{
cb =
wdApp.CommandBars.Add("TestToo-lBar",oMissing,oMissing,oMissi-ng);
cbb =
(CommandBarButton)cb.Controls.-Add(MsoControlType.msoControlB-utton,oMissing
,o
Missing,oMissing,oMissing);
cbb.Caption = "TestButton";
cbb.Click+=new
_CommandBarButtonEvents_ClickE-ventHandler(cbb_Click);
}

private Word.Application wdApp=null;
private void cbb_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
Debug.WriteLine("Button Clicked"+System.DateTime.Now.T-oString());
try
{
VBE.VBComponent vbc
=wdApp.ActiveDocument.VBProjec-t.VBComponents.Add(VBE.vbext_C-omponentType.v
be
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.T-oString());
}


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.
 

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