对象必需将文件附加到电子邮件时出错

时间:2016-07-06 21:50:19

标签: excel vba excel-vba

我目前正在尝试在excel vba中开发一个宏来发送附加到电子邮件的选定范围。由于我对vba的了解是零,我一直在浏览互联网,并且能够将这个脚本放在一起。目前,当我尝试附加文件时,我收到了所需对象的错误。

感谢你们给我的每一个帮助和反馈。

Sub AttachActiveSheetPDF()
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object

PdfFile = ActiveWorkbook.FullName
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"

With ActiveSheet.PageSetup
.Orientation = xlPortrait
.PrintArea = "$A$1:$P$84"
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile,       Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False,     OpenAfterPublish:=False
End With

On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0

Dim Ebody As String
Ebody = "<p>Buenas Dias,</p> <p>Adjunto es el documento.</p>"

With OutlApp.CreateItem(0)

.Subject = Quotation_de_Aerolinea
.To = "..."
.CC = "..."
.HTMLBody = Ebody
myAttachments.Add (PdfFile)

On Error Resume Next
.Display
Application.Visible = True
If Err Then
  MsgBox "E-mail was not sent", vbExclamation
Else
  MsgBox "E-mail successfully sent", vbInformation
End If
On Error GoTo 0
End With

End Sub

提前致谢

1 个答案:

答案 0 :(得分:1)

myAttachments不是Outlook中的集合 - 看起来可能来自其他一些代码。

尝试:

 .Attachments.Add PdfFile