【发布时间】:2019-01-09 08:42:08
【问题描述】:
尝试将附件链接到电子邮件时,标题中出现错误消息。附件存储在与公司“类型”相对应的文件夹名称中,这就是为什么我尝试添加一个 for 循环来从电子表格中检索“类型”。
Sub mailTest()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim olAttachmentLetter As Outlook.Attachments
Dim fileLocationLetter As String
Dim dType As String
For i = 2 To 3
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
Set olAttachmentLetter = olMail.Attachments
fileLocationLetter = "C:\...\user\Desktop\FileLocation"
letterName = "TestLetter1"
dType = Worksheets("Test1").Cells(i, 2).Value
mailBody = "Hello " _
& Worksheets("Test1").Cells(i, 4) _
& "," _
& Worksheets("BODY").Cells(2, 1).Value _
& Worksheets("BODY").Cells(3, 1).Value _
& Worksheets("BODY").Cells(4, 1).Value & " " & dType _
& Worksheets("BODY").Cells(5, 1).Value & " TTT" & dType & "xx18" _
& Worksheets("BODY").Cells(6, 1).Value _
& Worksheets("BODY").Cells(7, 1).Value
With olMail
.To = Worksheets("Test1").Cells(i, 5).Value
.Subject = Worksheets("Test1").Cells(i, 3).Value & " - "
.HTMLBody = "<!DOCTYPE html><html><head><style>"
.HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif; font-size: 13px}"
.HTMLBody = .HTMLBody & "</style></head><body>"
.HTMLBody = .HTMLBody & mailBody & "</body></html>"
''Adding attachment
.Attachments.Add fileLocationLetter & letterName & ".pdf"
.Display
'' .Send (Once ready to send)
End With
Set olMail = Nothing
Set olApp = Nothing
Next
End Sub
我在这里做错了什么?该文件存储在 'C:...\user\Desktop\FileLocation\TestLetter1.pdf'
谢谢。
【问题讨论】:
-
你已经验证了路径,你已经验证了名称,但是你还没有验证路径+文件名的组合。必须用“\”字符分隔:)
-
您正在运行什么操作系统来识别文件规范中的三点?这让我摸不着头脑。 blogs.msdn.microsoft.com/oldnewthing/20160202-00/?p=92953
-
@BillHileman 这(很可能)只是实际路径的占位符,由于某些隐私问题/等而被编辑。
-
我在 WIndows 10 命令提示符下尝试过,它没有返回错误并且什么都不做,即从 \Windows\User CD ...什么也不做。但是 CD .. 会按预期执行并移至父级。没有祖父母捷径。
-
它看起来不像那样,就像它只是做“C:\fake\path\to\your\file\directory\”的简写方式,IMO。跨度>
标签: vba excel email-attachments