Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将回调对象从.net发送到自定义Word宏_.net_Vb.net_Vba_Ms Word - Fatal编程技术网

如何将回调对象从.net发送到自定义Word宏

如何将回调对象从.net发送到自定义Word宏,.net,vb.net,vba,ms-word,.net,Vb.net,Vba,Ms Word,我有一些VB.net代码和一个自定义的Microsoft Word VBA宏。我试图将VB.net实例传递到宏中,以便从宏中对该对象运行函数。我似乎无法将对象从VB.net代码传递到宏 警告,我是VB和marcos这个词的新手。谢谢你的帮助 C#.net代码我用来创建word文档并将其传递给VB.net代码 private void BtnStartWord_Click(object sender, EventArgs e) { Microsoft.Office.In

我有一些VB.net代码和一个自定义的Microsoft Word VBA宏。我试图将VB.net实例传递到宏中,以便从宏中对该对象运行函数。我似乎无法将对象从VB.net代码传递到宏

警告,我是VB和marcos这个词的新手。谢谢你的帮助

C#.net代码我用来创建word文档并将其传递给VB.net代码

 private void BtnStartWord_Click(object sender, EventArgs e)
    {

        Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
        wdApp.Documents.Add(@"C:\template.dotm");
        wdApp.Visible = true;

        VBWord.Word wordVB = new VBWord.Word();

        wordVB.Connect(Handle.ToInt32(), wdApp);
    }
Public Class Word
    Public Sub Connect(ByVal plWinHandle As Long, _
                       ByVal pobjDocHandle As Object)

        RunMacro(pobjDocHandle, New Object() {"Connect", plWinHandle, Me})
    End Sub
    Private Sub RunMacro(ByVal oApp As Object, ByVal oRunArgs() As Object)
        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default Or System.Reflection.BindingFlags.InvokeMethod, Nothing, oApp, oRunArgs)
    End Sub
End Class
VB.net代码

 private void BtnStartWord_Click(object sender, EventArgs e)
    {

        Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
        wdApp.Documents.Add(@"C:\template.dotm");
        wdApp.Visible = true;

        VBWord.Word wordVB = new VBWord.Word();

        wordVB.Connect(Handle.ToInt32(), wdApp);
    }
Public Class Word
    Public Sub Connect(ByVal plWinHandle As Long, _
                       ByVal pobjDocHandle As Object)

        RunMacro(pobjDocHandle, New Object() {"Connect", plWinHandle, Me})
    End Sub
    Private Sub RunMacro(ByVal oApp As Object, ByVal oRunArgs() As Object)
        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default Or System.Reflection.BindingFlags.InvokeMethod, Nothing, oApp, oRunArgs)
    End Sub
End Class
在我的Word模板中,我有一些代码。代码位于TemplateProject->MicrosoftWord对象->ThisDocument中,如下所示

Option Explicit

Public Sub Connect(ByVal plWindow As Long, ByRef pobjControl As Object)
    Set goApp.App = Word.Application
    goApp.WindowHandle = plWindow
    goApp.ControlHandle = pobjControl
End Sub
使用模块->全局中定义的上述goApp

Option Explicit
Public goApp As New WordApp
当前,VB代码在上面的oApp.GetType().InvokeMember行上抛出错误。我认为它不喜欢我被传给它的论点。但是,我不知道如何通过自己

抛出的错误是

Message = "Exception has been thrown by the target of an invocation."
InnerException = {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}
我最终想要做的是用Word中的宏调用VB代码中的函数。类似下面的内容

Sub ApproveButton(ByVal control As IRibbonControl)
    '-----------------------------------------------------------------------------
    ' Description: This procedure sends the Approve action request back to the VB code
    '-----------------------------------------------------------------------------
    goApp.SendAction "Approve"
End Sub

如果不知道异常情况,就很难诊断。我建议使用以下标准方法之一从VBA调用.NET代码:

  • 创建一个COM可见的.NET DLL,在VBA中添加对它的引用,然后从VBA实例化“Word”类。见和
  • 使用