我想使用excel的邮件合并为所选记录发送电子邮件。如果没有电子邮件ID,则在电子邮件ID字段中有一个破折号。我收到运行时错误5630:
运行时错误'5630':excel无法合并可能的文档 通过邮件或传真分发,没有有效的邮件地址。选择 设置按钮,用于选择邮件地址数据字段。
option explicit
Sub MailMergeEmail()
'Note: this code requires a reference to the Word object model
Dim StrMMSrc As String, wdApp As New Word.Application, wdDoc As Word.Document, i As Long
Dim FirstRecord As Long, LastRecord As Long, DocName As String
wdApp.Visible = False
StrMMSrc = ThisWorkbook.FullName
Set wdDoc = wdApp.Documents.Open(Filename:=ThisWorkbook.Path & "\INPUT\ABCD COMPANY.docx", _
AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
With wdDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:=StrMMSrc, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _
"Data Source=StrMMSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `Data$`"
For i = FirstRecord To LastRecord
.Destination = wdSendToEmail
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
End With
.MailFormat = wdMailFormatHTML
.MailSubject = "Test"
.MailAddressFieldName = "E-MAIL ID"
.Execute Pause:=False
Next i
.MainDocumentType = wdNotAMergeDocument
End With
.Close SaveChanges:=False
End With
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
Application.ScreenUpdating = False
MsgBox "done"
End Sub
[/code]