Is it possible to insert VBA macros into Word 2010 with C# (with an exe)? I want to deploy a word2010 macro to our co-workers in my office. This my 2rd-3th question around this problem. Add-in or module method has failed. Very difficult to them. I need one-click exe to set a bunch of macros in to normal.dot. Thanks
Asked
Active
Viewed 1,772 times
0
-
May be you should ask windows administrators on how to roll out changes. – Peter Schuetze Dec 03 '13 at 20:29
1 Answers
2
Yes, you can create a new VBA module, insert your code and run it. I use this, because macro is in some cases faster then C# code (yes, unbelievable, but true; I don't know why). Here is an example:
//creating a new VBA module (must be a reference to Microsoft.Vbe.Interop)
Microsoft.Vbe.Interop.VBComponent vbaModule =
docComp.VBProject.VBComponents.Add(
Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
//insert VBA macro code
vbaModule.CodeModule.AddFromString(string_with_your_vba_code);
//run the macro
Microsoft.Office.Interop.Word.Application.GetType().InvokeMember("Run",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
Microsoft.Office.Interop.Word.Application,
new Object[] { "name_of_your_main_sub" });
The magic of running macro is described here.

Roman Plischke
- 1,042
- 7
- 14
-
Thank you @Roman Plischke. Here is the code i wrote in VBA (word2010) http://stackoverflow.com/questions/20323598/late-binding-to-avoid-user-defined-type-not-defined-error – caglaror Dec 05 '13 at 14:42