[英]VBA Macro no longer works when used as a COM Add-In (VB.NET) (Mail Merge function)
我有下面的代碼。 最初,代碼是我構建的VBA宏。 最終效果完美(將doc文檔作為電子郵件發送給所需范圍的收件人,遍歷每一行)。 該函數從代碼中的Sub SendIt_Click
(最后一個子)開始。 其余的用於加載項。 當我單擊Excel中的按鈕時,MsgBox起作用了,但是代碼沒有發送任何東西。 它可以在Excel VBA中運行,但是我不知道為什么它在這里不起作用。
更新:它確實打開doc一詞,只是不發送電子郵件。
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
應OP的要求,我正在做的只是事后總結:)
如有疑問,請自行調試代碼。 逐步執行代碼,但是在這種情況下,當您測試VSTO加載項的代碼時,我通常在代碼中放幾個消息框,這樣我就知道哪一行在執行而哪一行不在執行。
Op遵循了這種方法,並找到了罪魁禍首的兩條線。
.To = xRecipient
和
myxl.Application.Wait(Now + TimeValue("00:00:20"))
第一個失敗,因為該字段需要一個字符串值。 它使用
.To = xRecipient.Value.ToString()
我建議對.CC
字段也執行相同的操作。
關於其他Now + TimeValue("00:00:20")
計算不正確。 那是因為您有“ +”號。 嘗試在VB.Net中這樣做
MessageBox.Show(Now + TimeValue("00:00:20"))
替代方法是使用
myxl.Application.Wait(Now.AddSeconds(20))
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.