Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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
Vb.net 从DLL停止VBA执行_Vb.net_Vba_Excel - Fatal编程技术网

Vb.net 从DLL停止VBA执行

Vb.net 从DLL停止VBA执行,vb.net,vba,excel,Vb.net,Vba,Excel,在将Excel VBA应用程序转换为VB.NET的过程中,我知道我不能在DLL中使用End语句,因为它只能由启动它的进程停止。相反,DLL似乎必须引发一个事件,由Excel中的VBA代码处理 我认为,除了应该检测到引发的事件以便立即停止执行的部分之外,它大部分都正常工作。以下是我目前得到的信息: VBA调用的DLL过程: Public Class Class1 Sub DLLentry() Dim ClStop As New ClassStop AddHa

在将Excel VBA应用程序转换为VB.NET的过程中,我知道我不能在DLL中使用
End
语句,因为它只能由启动它的进程停止。相反,DLL似乎必须引发一个事件,由Excel中的VBA代码处理

我认为,除了应该检测到引发的事件以便立即停止执行的部分之外,它大部分都正常工作。以下是我目前得到的信息:

VBA调用的DLL过程:

Public Class Class1
    Sub DLLentry()
        Dim ClStop As New ClassStop
        AddHandler StopExecution, AddressOf ClStop.stopExec
        DLLprocedure1()
    End Sub
End Class
执行大多数操作的过程:

Module Module1
    Public Event StopExecution()
    Sub DLLprocedure1()
        'Operations that in certain circumstances must stop the execution
        RaiseEvent StopExecution()
    End Sub
End Module
应将引发的事件通信到Excel的类:

Imports System.Runtime.InteropServices

<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
<Guid("28C7DCE1-90EF-4a30-AF7F-4187F9FFFDEB")>
Public Interface ICoreEvents
    <DispId(1)>
    Sub StopExecutionCl()
End Interface

<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
<Guid("86CE5E8D-777D-4cd5-8A7D-7F58737F1DB4")>
Public Interface ICore
    Sub stopExec()
End Interface

<ComVisible(True)>
<ClassInterface(ClassInterfaceType.None)>
<ComDefaultInterface(GetType(ICore))>
<ComSourceInterfaces(GetType(ICoreEvents))>
<Guid("C58721B1-15B3-4eeb-9E1E-BCDA33D38EE6")>
Public Class ClassStop
    Implements ICore
    <ComVisible(False)>
    Public Delegate Sub OnStopExecutionCl()
    Public Event StopExecutionCl As OnStopExecutionCl
    Sub stopExec() Implements ICore.stopExec
        MsgBox("Checkpoint 1")
        RaiseEvent StopExecutionCl()
    End Sub
End Class
以及应捕获事件并停止执行的类模块:

Sub StartingProcedure()
    Dim oMyApp1 As New Class1
    Call oMyApp1.activateEvent

    Dim oMyApp2 As New DLLname.Class1
    Call oMyApp2.DLLentry
End Sub
Public WithEvents cls1 As DLLname.ClassStop

Private Sub cls1_StopExecutionCl()
    MsgBox "Checkpoint 2"
    End
End Sub

Public Sub activateEvent()
    Set cls1 = New DLLname.ClassStop
End Sub
DLL“DLLname.DLL”注册为COM并在Excel中引用。我知道Excel可以正确地检测DLL中的事件声明,因为我可以从类模块上的下拉菜单中选择它

问题是,即使多次到达“检查点1”(引发事件),也没有到达“检查点2”

我错过了什么?这是实现我想要的最好方式吗


另外,我不确定,但我不能在评论中使用@Tim Williams指南来实现
Guid
定义和
接口
ICore

工作代码

在DLL中:

Imports System.Runtime.InteropServices

<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
<Guid("28C7DCE1-90EF-4a30-AF7F-4187F9FFFDEB")>
Public Interface ICoreEvents
    <DispId(1)>
    Sub StopExecutionCl()
End Interface

<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
<Guid("86CE5E8D-777D-4cd5-8A7D-7F58737F1DB4")>
Public Interface ICore
    Sub DLLentry()
End Interface

<ComVisible(True)>
<ClassInterface(ClassInterfaceType.None)>
<ComDefaultInterface(GetType(ICore))>
<ComSourceInterfaces(GetType(ICoreEvents))>
<Guid("C58721B1-15B3-4eeb-9E1E-BCDA33D38EE6")>
Public Class Class1
    Implements ICore
    Public Event StopExecutionCl()

    Sub stopExec()
        MsgBox("Checkpoint 1")
        RaiseEvent StopExecutionCl()
    End Sub

Sub DLLentry() Implements ICore.DLLentry
        AddHandler StopExecution, AddressOf stopExec
        DLLprocedure1()
    End Sub
End Class
在Excel的VBA类模块中
Class1

Public WithEvents cls1 As DLLname.Class1

Private Sub cls1_StopExecutionCl()
    MsgBox "Checkpoint 2"
    End
End Sub
在模块中:

Module Module1
    Public Event StopExecution()
    Sub DLLprocedure1()
        'Operations that in certain circumstances must stop the execution
        RaiseEvent StopExecution()
    End Sub
End Module
Sub StartingProcedure()
    Dim oMyApp1 As New Class1
    Set oMyApp1.cls1 = New DLLname.Class1
    Call oMyApp1.cls1.DLLentry
End Sub

不确定我是否完全遵循了,因为我从未这样做过,但事件处理VBA类实例中的DLL对象实例
cls1
不是与
myyapp2
@TimWilliams不同吗?是的,它们引用DLL中的不同类。我尝试将过程
DLLentry
移动到
ClassStop
,从而消除
Class1
,使其成为一个单独的类,但结果是一样的。不过,在DLL的不同实例中,它们不是不同的类吗?@TimWilliams你是对的。我不太清楚不同实例的概念。我被
调用myyapp1.cls1.DLLentry
替换,现在它可以工作了。非常感谢。