我想從一個文件夾中發送一些文件到一個固定的電子郵件地址,這些文件需要在個別電子郵件中發送,文件名是隨機的。展望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對象庫的引用? –