如何在c#中获得Word VBA函数的结果

本文关键字:VBA Word 函数 结果 | 更新日期: 2023-09-27 18:20:06

我需要打开一个Microsoft Office Word文档,并获得该文档中某个模块的函数的结果:

VBA模块(主模块):

Option Explicit
Public Function GetResult() As String
    GetResult = "My Expected Result"
End Function

我尝试了一些代码,最新的是:[参考]

using System.Reflection;
using System.Runtime.InteropServices;
using MsWord = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
...
var wordApp = new MsWord.Application();
wordApp.DisplayAlerts = MsWord.WdAlertLevel.wdAlertsNone;
wordApp.Visible = true;
wordApp.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
MsWord._Document wordDoc = wordApp.Documents.Open(ref fileNameObject,  ref falseValue, ref trueValue, ref falseValue,
    ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
    ref missingValue, ref missingValue, ref falseValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue);    
object n;
n = wordApp.Run("GetResult");
// or
n = wordApp.Run("MainModule.GetResult");
// or 
n = RunMacro(wordApp, new Object[] {"MainModule.GetResult"});
// or
n = RunMacro(wordApp, new Object[] {"GetResult"});
// or
n = wordApp.Run("MainModule.GetResult", ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue);
...
private static object RunMacro(object oApp, object[] oRunArgs)
{
    return oApp.GetType()
        .InvokeMember("Run", BindingFlags.Default | BindingFlags.InvokeMethod, null, oApp, oRunArgs);
}

RunMacro行的错误为:

调用的目标引发了异常
[内部异常]:无法运行指定的宏

堆栈跟踪是:

at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at ConsoleApplicationTest.Program.RunMacro(Object oApp, Object[] oRunArgs) in c:'..'Visual Studio 2013'Projects'ConsoleApplicationTest'ConsoleApplicationTest'Program.cs:line 57
at ConsoleApplicationTest.Program.Main(String[] args) in c:'..'Visual Studio 2013'Projects'ConsoleApplicationTest'ConsoleApplicationTest'Program.cs:line 32

注意:
Office 2013将My Word文档另存为Office 97-2003文档。
宏设置是启用所有宏
并且还检查了对VBA工程对象模型的信任访问。

如有任何帮助,我们将不胜感激。

如何在c#中获得Word VBA函数的结果

以下内容在vb.net中运行得很好。我相信你可以很容易地将其翻译成C#:

VB.Net:

Public Class Form1
    Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
        Dim wdApp As New Microsoft.Office.Interop.Word.Application()
        Dim wdDoc As Microsoft.Office.Interop.Word.Document
        wdDoc = wdApp.Documents.Open("C:'SO'Abc.docm")
        Dim s As String = wdApp.Run("myFunc", "Hello World!")
        MsgBox(s)
        wdDoc.Close(False) : wdApp.Quit()
    End Sub
End Class

VBA Word:

Function myFunc(str As String) As String
    myFunc = str
End Function

p.s:VS2015,Word2007,Office12

编辑令人惊讶的是,当我将您在代码中添加的安全设置(即... = msoAutomationSecurityForceDisable)添加到代码中时,我失败了。然后我转到MSDN的页面,发现:

MsoAutomationSecurityLow打开所有宏,并且是默认值当你启动程序时。MsoAutomationSecurityForceDisable禁用以编程方式打开的所有文件中的所有宏显示任何安全警告。

据我所知,它必须是MsoAutomationSecurityLow。这个在这里又起作用了。