Ms word 在c中使用互操作在word中插入并运行宏#

Ms word 在c中使用互操作在word中插入并运行宏#,ms-word,macros,Ms Word,Macros,如何使用c#中的Interop将宏插入并运行到word文档中?您尝试过的任何操作?我有一个宏,但找不到任何使用Interop插入宏的方法。 using System.Runtime.InteropServices; using Microsoft.Vbe.Interop; using Word = Microsoft.Office.Interop.Word.Application; string file1, file2; var word = new Word { Visible = tr

如何使用c#中的Interop将宏插入并运行到word文档中?

您尝试过的任何操作?我有一个宏,但找不到任何使用Interop插入宏的方法。
using System.Runtime.InteropServices;
using Microsoft.Vbe.Interop;
using Word = Microsoft.Office.Interop.Word.Application;

string file1, file2;

var word = new Word { Visible = true };
var doc = word.Documents.Open(file1);

var project = doc.VBProject;
var module = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);

var script = string.Format(
@"Sub MacroTest()
Const path1 As String = ""{0}""
Set doc1 = Documents.Open(path1)
End Sub", file2);

module.CodeModule.AddFromString(script);

word.Run("MacroTest");