我正在尝试将文件夹中的某些文件发送到固定的电子邮件地址,这些文件需要在各个电子邮件中发送,文件名是随机的。
这个话题让我开始了:
Send individual emails to predefined set of people with all files in a folder
我稍微改变了代码以满足我的需要,但是当我运行宏时它不会发送文件。我确定这是一个简单的错误,但我的知识有限!
这是我的代码:
Option Explicit
Const SOURCE_FOLDER As String = "C:\Users\Me\Desktop\Test"
Const RECIP_A As String = "me@hotmail.com"
Const EMAIL_BODY As String = "Please find attached file. Thanks and Regards, ABC"
Sub SendPDFs()
On Error GoTo ErrorHandler
Dim fileName As String
fileName = Dir(SOURCE_FOLDER)
Do While Len(fileName) > 0
Call CreateEmail(SOURCE_FOLDER & fileName)
fileName = Dir
Loop
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Function CreateEmail(fileName As String)
Dim olApp As Outlook.Application
Dim msg As Outlook.MailItem
' create email
Set olApp = Outlook.Application
Set msg = olApp.CreateItem(olMailItem)
' set properties
With msg
.Body = EMAIL_BODY
.Recipients.Add (RECIP_A)
.Attachments.Add fileName
.Send
End With
End Function
答案 0 :(得分:1)
啊!代码唯一的问题是
Const SOURCE_FOLDER As String =" C:\ Users \ Me \ Desktop \ Test"
将其更改为
Const SOURCE_FOLDER As String = "C:\Users\Me\Desktop\Test\"
现在试试吧。我尝试并测试了它并且它有效。
还要确保已添加对Outlook对象库的引用。