附加excel文件而不出现“验证路径和文件名是否正确”错误?

ix0qys7i  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(122)

我尝试做的是循环发送一封电子邮件给一个列表中的人,每个人都收到自己的excel文件。第一部分还没有设置,所以我有占位符的信息,谁去,但我有附件的问题。该程序没有附件行工作正常,但当我添加,我得到了前面提到的错误,我难倒如何修复它。

Sub AttachAndEmail()
Dim fileDirectory As String
Dim fileCriteria As String
Dim fileName As String
Dim emailApplication As Object
Dim emailItem As Object

On Error Resume Next
Set emailApplication = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set emailApplication = CreateObject("Outlook.Application")
Set emailItem = emailApplication.CreateItem(0)
On Error GoTo 0

Application.ScreenUpdating = False

fileDirectory = "C:\Users\DW1085\Downloads\a\"

fileName = Dir(fileDirectory)

Do While Len(fileName) > 0
    
    emailItem.to = "Myname@email.com"
    
    emailItem.Subject = "WowweWow"
    
    emailItem.Body = "Yup"

    emailItem.Attachments.Add fileName
    
    emailItem.Display

    fileName = Dir
Loop

Application.ScreenUpdating = True

End Sub
hlswsv35

hlswsv351#

Attachments.Add方法在Attachments集合中创建一个新附件。附件的源可以是一个文件(由带有文件名的完整文件系统路径表示),也可以是构成附件的Outlook项目。因此,您需要传递要附加的文件的完整路径。例如:

Sub AddAttachment() 
 Dim myItem As Outlook.MailItem 
 Dim myAttachments As Outlook.Attachments 
 
 Set myItem = Application.CreateItem(olMailItem) 
 Set myAttachments = myItem.Attachments 
 myAttachments.Add "C:\Test.doc", olByValue, 1, "Test" 
 myItem.Display 
End Sub

相关问题