Microsoft 365 and Office | Development | Other
Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have question on VBA code.
I want to write a code to send a mail with attachment.
But got below error message on .Attchments.Add
Problem is the file name is changing everyday, I cannot write a certain file path there..
in this case, how should I do?
Kindly advise.
Sub Send_Email()
'This code is early binding i.e in Tools >
'Reference >You have check "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY"
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim FileName As String
Dim FilePath As String
Dim Name As String
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
FilePath = "\\xxx\xxxx\xxxxx\xxxx\xxxxx\xxxxx\xxxxx\xxxxx\"
Name = Range("I1").Value
FileName = Dir(FilePath & Name & ".pdf")
With OutlookMail
.BodyFormat = olFormatHTML
.Display
.HTMLBody = "Hi, <br> <br> Please see attached file! <br> <br> Thanks!"
.To = ""
.CC = ""
.BCC = Range("I11").Value
.Subject = Range("I1").Value
.Display
End With
End Sub