1

我有以下代码,我的目标是使用文本文件作为模板向 excel 文档中的人员列表发送自动电子邮件:

Set objMessage = CreateObject("CDO.Message") 
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("F:\Billing_Common\autoemail").Files
  If LCase(fso.GetExtensionName(f)) = "xls" Then
    Set wb = app.Workbooks.Open(f.Path)

set sh = wb.Sheets("Auto Email Script")
row = 2
email = sh.Range("A" & row)
subject = "Billing"
LastRow = sh.UsedRange.Rows.Count

For r = row to LastRow
    If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then 
    objMessage.Subject = "Billing: Meter Read" 
    objMessage.From = "billing@energia.ie" 
    objMessage.To = email

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim emailText                                   
Set emailText = fso.OpenTextFile("F:\Billing_Common\autoemail\Script\Email.txt", ForReading)                                        
BodyText = emailText.ReadAll

    objMessage.TextBody = emailText

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = CdoSendUsingPort


'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "ADDRESS OF SERVER HERE"

'Server port
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

objMessage.Configuration.Fields.Update
objMessage.Send

    End if
Next

emailText.Close
Set emailText = Nothing
Set fso = Nothing
wb.Close
End If
Next

它在 objMessage.TextBody 上抛出一个错误,说类型不匹配。如果有人可以帮助我,将不胜感激!

谢谢!

4

2 回答 2

1

要发送内联图像,您需要创建一个HTMLBody而不是 aTextBody并在图像中添加一个RelatedBodyPart(参见此处):

Set msg = CreateObject("CDO.Message")
...
msg.HTMLBody = "<html>" & vbLf & _
               "<head><title>Test</title></head>" & vbLf & _
               "<body><p><img src='foo.jpg'></p></body>" & vbLf & _
               "</html>"
msg.AddRelatedBodyPart "C:\path\to\your.jpg", "foo.jpg", 0
于 2013-07-03T10:06:35.437 回答
0

在该行之后BodyText = emailText.ReadAll,您应该分配那个,而不是文件(“emailText”是上一行编辑的TextFile那个),这就是它抱怨类型不匹配的原因......Openfso

所以只需替换objMessage.TextBody = emailTextobjMessage.TextBody = BodyText,它应该可以工作......

于 2015-04-17T18:20:16.817 回答