VBA宏在用作COM加载项(VB.NET)(邮件合并功能)时不再工作

VBA宏在用作COM加载项(VB.NET)(邮件合并功能)时不再工作,vb.net,visual-studio-2010,excel,vba,Vb.net,Visual Studio 2010,Excel,Vba,我有下面的代码。最初,代码是我构建的VBA宏。它最终工作得非常完美(将word文档作为电子邮件发送给所需范围的收件人,并在每一行中进行迭代)。该函数从代码中的子项SendIt\u单击(最后一个子项)开始。其余部分用于外接程序。当我在Excel中单击该按钮时,MsgBox正在运行,但代码没有发送任何内容。它在Excel VBA中工作,但我不知道为什么它不在这里工作 更新:它确实打开word文档,只是不发送电子邮件 Imports Extensibility Imports System.Runti

我有下面的代码。最初,代码是我构建的VBA宏。它最终工作得非常完美(将word文档作为电子邮件发送给所需范围的收件人,并在每一行中进行迭代)。该函数从代码中的
子项SendIt\u单击
(最后一个子项)开始。其余部分用于外接程序。当我在Excel中单击该按钮时,MsgBox正在运行,但代码没有发送任何内容。它在Excel VBA中工作,但我不知道为什么它不在这里工作

更新:它确实打开word文档,只是不发送电子邮件

Imports Extensibility
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core

<GuidAttribute("209AD741-0B95-4931-80CF-4DCE33B761C9"), ProgIdAttribute("MailMerge.Connect")> _
Public Class Connect

    Implements Extensibility.IDTExtensibility2

    Private applicationObject As Object
    Private addInInstance As Object
    Dim WithEvents SendIt As CommandBarButton

    Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
        On Error Resume Next
        ' Notify the user you are shutting down, and delete the button.
        MsgBox("MailMerge Add-in is unloading.")
        SendIt.Delete()
        SendIt = Nothing

    End Sub

    Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
    End Sub

    Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete

        Dim oCommandBars As CommandBars
        Dim oStandardBar As CommandBar

        On Error Resume Next
        ' Set up a custom button on the "Standard" command bar.
        oCommandBars = applicationObject.CommandBars
        If oCommandBars Is Nothing Then
            ' Outlook has the CommandBars collection on the Explorer object.
            oCommandBars = applicationObject.ActiveExplorer.CommandBars
        End If

        oStandardBar = oCommandBars.Item("Standard")
        If oStandardBar Is Nothing Then
            ' Access names its main toolbar Database.

            oStandardBar = oCommandBars.Item("Database")

        End If

        ' In case the button was not deleted, use the exiting one.
        SendIt = oStandardBar.Controls.Item("My Custom Button")
        If SendIt Is Nothing Then

            SendIt = oStandardBar.Controls.Add(1)
            With SendIt
                .Caption = "Send to Mail Group with Outlook"
                .Style = MsoButtonStyle.msoButtonCaption

                ' The following items are optional, but recommended. 
                ' The Tag property lets you quickly find the control 
                ' and helps MSO keep track of it when more than
                ' one application window is visible. The property is required
                ' by some Office applications and should be provided.

                .Tag = "MailMerge"

                ' The OnAction property is optional but recommended. 
                ' It should be set to the ProgID of the add-in, so that if
                ' the add-in is not loaded when a user clicks the button,
                ' MSO loads the add-in automatically and then raises
                ' the Click event for the add-in to handle. 

                .OnAction = "!<MyCOMAddin.Connect>"

                .Visible = True
            End With
        End If

        ' Display a simple message to show which application you started in.
        MsgBox("Started in " & applicationObject.Name & ".")

        oStandardBar = Nothing
        oCommandBars = Nothing

    End Sub

    Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection

        On Error Resume Next
        If RemoveMode <> Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
           Call OnBeginShutdown(custom)

        applicationObject = Nothing

    End Sub

    Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

        MsgBox("On Connection In MailMerge")
        applicationObject = application
        addInInstance = addInInst

        ' If you aren't in startup, manually call OnStartupComplete.
        If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup) Then _
           Call OnStartupComplete(custom)

    End Sub

    Private Sub SendIt_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles SendIt.Click
        MsgBox("SendIt button was pressed!")

        'Dimension variables.
        Dim OL As Object, MailSendItem As Object
        Dim myxl As Excel.Application
        Dim ws As Excel.Worksheet
        Dim wd As Word.Application
        Dim toRange = InputBox("Input cell range in R1:C1 format.", "Input range", "B3:B4")
        Dim subj = InputBox("Input subject.", "Input subject", "TESTING")

        wd = CreateObject("Word.Application")

        Dim doc As Word.Document
        'On Error Resume Next

        'Assigns Word file to send
        wd = GetObject(, "Word.Application")

        If wd Is Nothing Then
            wd = CreateObject("Word.Application")
            'blnWeOpenedWord = True (MAY NOT NEED THIS)
        End If

        doc = wd.Documents.Open _
        (FileName:="H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.doc", ReadOnly:=False)
        'Set itm = doc.MailEnvelope.Item

        'Starts Outlook session
        OL = CreateObject("Outlook.Application")
        MailSendItem = doc.MailEnvelope.Item

        myxl = GetObject(, "Excel.application")
        ws = myxl.ActiveSheet

        'Creates message
        For Each xRecipient In ws.Range(toRange)
            With MailSendItem
                .Subject = subj
                .To = xRecipient
                .Cc = xRecipient.Offset(0, 5)
                .Attachments.Add("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")
                .Send()
            End With
            doc.Close(SaveChanges:=0)

            wd = GetObject(, "Word.Application")

            doc = wd.Documents.Open _
         (FileName:="H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.doc", ReadOnly:=False)
            MailSendItem = doc.MailEnvelope.Item
            myxl.Application.Wait(Now + TimeValue("00:00:20"))
        Next xRecipient

        'Ends Outlook session
        OL = Nothing

    End Sub

End Class
导入可扩展性
导入System.Runtime.InteropServices
导入Microsoft.Office.Interop
导入Microsoft.Office.Core
_
公共类连接
实现可扩展性。IDTExtensibility2
私有应用程序对象作为对象
作为对象的私有附加
Dim With Events将其作为命令按钮发送
公共子OnBeginShutton(ByRef自定义为System.Array)实现可扩展性.IDTExtensibility2.OnBeginShutton
出错时继续下一步
'通知用户您正在关闭,并删除该按钮。
MsgBox(“邮件合并加载项正在卸载”)
SendIt.Delete()
SendIt=无
端接头
公共子OnAddInsUpdate(ByRef custom As System.Array)实现可扩展性.IDTExtensibility2.OnAddInsUpdate
端接头
Public Sub OnStartupComplete(ByRef自定义为System.Array)实现可扩展性。IDTExtensibility2.OnStartupComplete
将oCommandBars设置为命令栏
作为命令栏的Dim oStandardBar
出错时继续下一步
'在“标准”命令栏上设置自定义按钮。
oCommandBars=applicationObject.commandbar
如果oCommandBars不算什么,那么
'Outlook在资源管理器对象上具有CommandBars集合。
oCommandBars=applicationObject.ActiveExplorer.CommandBars
如果结束
oStandardBar=ocommandbar.项目(“标准”)
如果oStandardBar什么都不是
'Access将其主工具栏命名为数据库。
oStandardBar=oCommandBars.Item(“数据库”)
如果结束
'如果按钮未被删除,请使用退出按钮。
SendIt=oStandardBar.Controls.Item(“我的自定义按钮”)
如果这算不了什么
SendIt=oStandardBar.Controls.Add(1)
用SendIt
.Caption=“使用Outlook发送到邮件组”
.Style=MsoButtonStyle.msoButtonCaption
'以下项目是可选的,但建议使用。
'标记属性允许您快速查找控件
并帮助MSO在超过
'一个应用程序窗口可见。该属性是必需的
“由一些办公应用程序提供,并应提供。
.Tag=“MailMerge”
'OnAction属性是可选的,但建议使用。
'它应该设置为外接程序的ProgID,以便
'当用户单击按钮时,加载项未加载,
'MSO自动加载外接程序,然后引发
'外接程序要处理的单击事件。
.OnAction=“!”
.Visible=True
以
如果结束
'显示一条简单消息,显示您在哪个应用程序中启动。
MsgBox(“在“&applicationObject.Name&”中启动”)
oStandardBar=无
oCommandBars=无
端接头
Public Sub-OnDisconnection(ByVal RemoveMode作为Extensibility.ext_DisconnectMode,ByRef custom作为System.Array)实现Extensibility.IDTExtensibility2.OnDisconnection
出错时继续下一步
如果RemoveMode Extensibility.ext\u DisconnectMode.ext\u dm\u HostShutdown_
调用BeginShutdown(自定义)
applicationObject=Nothing
端接头
Public Sub OnConnection(ByVal应用程序作为对象,ByVal connectMode作为可扩展性.ext_connectMode,ByVal AddInInt作为对象,ByRef自定义作为系统.Array)实现可扩展性.IDTExtensibility2.OnConnection
MsgBox(“邮件合并中的连接”)
applicationObject=应用程序
附加值=附加值
'如果您不在启动中,请手动调用OnStartupComplete。
If(connectMode Extensibility.ext\u connectMode.ext\u cm\u Startup)则_
启动时调用完成(自定义)
端接头
Private Sub SendIt_Click(ByVal Ctrl为Microsoft.Office.Core.CommandBarButton,ByRef CancelDefault为布尔值)处理SendIt。单击
MsgBox(“已按下SendIt按钮!”)
'维度变量。
Dim OL作为对象,MailSendItem作为对象
Dim myxl作为Excel.Application
将ws设置为Excel.Worksheet
Dim wd作为Word.Application
Dim toRange=输入框(“R1:C1格式的输入单元格范围”,“输入范围”,“B3:B4”)
Dim subj=输入框(“输入主题”,“输入主题”,“测试”)
wd=CreateObject(“Word.Application”)
Dim doc作为Word.Document
'出现错误时,请继续下一步
'指定要发送的Word文件
wd=GetObject(,“Word.Application”)
如果wd什么都不是,那么
wd=CreateObject(“Word.Application”)
'blnWeOpenedWord=True(可能不需要此选项)
如果结束
doc=wd.Documents.Open_
(文件名:=“H:\Thinks Pieces\Small Cap Liquidity\A Close Look on Small Cap Liquidity.doc”,只读:=False)
'设置itm=doc.mailevelope.Item
'启动Outlook会话
OL=CreateObject(“Outlook.Application”)
MailSendItem=doc.mailevelope.Item
myxl=GetObject(,“Excel.application”)
ws=myxl.ActiveSheet
'创建消息
对于ws.Range(toRange)中的每个xRecipient
使用MailSendItem
主语,主语
.To=xRecipient
.Cc=xRecipient.Offset(0,5)
.To = xRecipient
myxl.Application.Wait(Now + TimeValue("00:00:20"))
.To = xRecipient.Value.ToString()
MessageBox.Show(Now + TimeValue("00:00:20"))
myxl.Application.Wait(Now.AddSeconds(20))