create vba macros and run in Word
   taken from Example05.cs
private void Example05Main()
   // start word and turn off msg boxes
   Word.Application wordApplication = new Word.Application();
   wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

   // add a new document
   Word.Document newDocument = wordApplication.Documents.Add();

   // add new module and insert macro
   // the option "Trust access to Visual Basic Project" must be set
   NetOffice.VBIDEApi.CodeModule module =                  
vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic" +
"created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); //save the document string fileExtension = GetDefaultExtension(wordApplication); object documentFile =
string.Format("{0}\\Example05{1}", Application.StartupPath, fileExtension); newDocument.SaveAs(documentFile); //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); FinishDialog fDialog = new FinishDialog("Document saved.", documentFile.ToString()); fDialog.ShowDialog(this); } #region Helper /// <summary> /// returns the valid file extension for the instance. for example ".doc" or ".docx" /// </summary> /// <param name="application">the instance</param> /// <returns>the extension</returns> private static string GetDefaultExtension(Word.Application application) { double version = Convert.ToDouble(application.Version, CultureInfo.InvariantCulture); if (version >= 12.00) return ".docx"; else return ".doc"; } #endregion

Last edited Jun 13, 2012 at 9:00 AM by SebastianDotNet, version 9


cgh_chen Aug 26, 2013 at 10:49 AM 

cgh_chen Aug 26, 2013 at 10:38 AM 
I run the code above in a NetOffice AddIn.

cgh_chen Aug 26, 2013 at 10:37 AM 
到 Visual Basic Project 的程序访问不被信任
=== Programmatic Access To Visual Basic Project Is Not Trusted

cgh_chen Aug 26, 2013 at 10:37 AM 
System.Runtime.InteropServices.COMException (0x80004005): See inner exception(s) for details. ---> System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.Runtime.InteropServices.COMException (0x800A17B4): 到 Visual Basic Project 的程序访问不被信任。
--- 内部异常堆栈跟踪的结尾 ---
在 System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
在 System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
在 System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
在 NetOffice.Invoker.PropertyGet(COMObject comObject, String name, Object[] paramsArray)
在 NetOffice.Invoker.PropertyGet(COMObject comObject, String name, Object[] paramsArray)
在 NetOffice.WordApi._Document.get_VBProject()