【问题标题】:Insert and run macro into word using interop in c#使用 c# 中的互操作将宏插入并运行到 word 中
【发布时间】:2017-05-19 14:39:08
【问题描述】:

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

【问题讨论】:

  • 你尝试过什么?
  • 我有一个宏,但我没有找到使用互操作插入该宏的任何方法。

标签: c# vba ms-word word-interop


【解决方案1】:
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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多