我想从一个文件夹中发送一些文件到一个固定的电子邮件地址,这些文件需要在个别电子邮件中发送,文件名是随机的。展望VB宏 - 我做错了什么?
本主题让我开始:
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 = "[email protected]"
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
代码似乎没有问题。顺便说一句你有没有添加对Outlook对象库的引用? –