Solutionmatt
New Member
- Joined
- Feb 2, 2022
- Messages
- 16
- Office Version
- 365
- 2021
- Platform
- Windows
Morning all!
I have this VBA, and I am happy with it, however it lacks 1 thing, I need to save as an XLSM file aswell as the PDF it saves to the file extension path?
I can't for the life of me get it to do both? Can one of you guys solve this for me? (VBA below)
Private Sub btnEmail_Click()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
Dim Login_EmailAddress, Login_EmailPassword, SMTPServer As String
Dim ServerPort As Integer
Dim To_Email, CC_Email, BCC_Email, Email_Subject, Email_Body, Attachment_Path As String
Dim FileExtn As String
FileExtn = ".PDF"
SMTPServer = "smtp.gmail.com"
ServerPort = 465
Login_EmailAddress = Sheet1.Range("O2").Value
Login_EmailPassword = Sheet1.Range("O3").Value
To_Email = Sheet1.Range("L2").Value
CC_Email = Sheet1.Range("L3").Value
BCC_Email = Sheet1.Range("L4").Value
Attachment_Path = Sheet1.Range("L5").Value
Email_Subject = Sheet1.Range("L6").Value
Email_Body = Sheet1.Range("L7").Value
If Sheet1.Range("L5").Value <> "" Then
Sheet1.Calculate
Attachment_Path = VBA.UCase(Sheet1.Range("L5").Value)
If VBA.InStr(Attachment_Path, FileExtn) > 0 Then Attachment_Path = VBA.Replace(Attachment_Path, FileExtn, "")
If Sheet1.SendRangeFLAG.Value = True Then
Sheet1.Range("A1:I37").ExportAsFixedFormat xlTypePDF, Filename:=Attachment_Path
End If
End If
With myMail.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = ServerPort
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Login_EmailAddress
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Login_EmailPassword
.Update
End With
With myMail
.From = Login_EmailAddress
.Subject = Email_Subject
.To = To_Email
.CC = CC_Email
.BCC = BCC_Email
.TextBody = "Dear soon to be Mr & Mrs" & vbNewLine & _
"" & vbNewLine & _
"Please find attached you latest Installment Receipt for your wedding."
If Attachment_Path <> "" Then .AddAttachment Attachment_Path & FileExtn
End With
On Error Resume Next
myMail.Send
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical
Else
MsgBox ("WOOHOO! IT'S ON IT'S WAY!"), vbInformation
End If
Set myMail = Nothing
End Sub
I have this VBA, and I am happy with it, however it lacks 1 thing, I need to save as an XLSM file aswell as the PDF it saves to the file extension path?
I can't for the life of me get it to do both? Can one of you guys solve this for me? (VBA below)
Private Sub btnEmail_Click()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
Dim Login_EmailAddress, Login_EmailPassword, SMTPServer As String
Dim ServerPort As Integer
Dim To_Email, CC_Email, BCC_Email, Email_Subject, Email_Body, Attachment_Path As String
Dim FileExtn As String
FileExtn = ".PDF"
SMTPServer = "smtp.gmail.com"
ServerPort = 465
Login_EmailAddress = Sheet1.Range("O2").Value
Login_EmailPassword = Sheet1.Range("O3").Value
To_Email = Sheet1.Range("L2").Value
CC_Email = Sheet1.Range("L3").Value
BCC_Email = Sheet1.Range("L4").Value
Attachment_Path = Sheet1.Range("L5").Value
Email_Subject = Sheet1.Range("L6").Value
Email_Body = Sheet1.Range("L7").Value
If Sheet1.Range("L5").Value <> "" Then
Sheet1.Calculate
Attachment_Path = VBA.UCase(Sheet1.Range("L5").Value)
If VBA.InStr(Attachment_Path, FileExtn) > 0 Then Attachment_Path = VBA.Replace(Attachment_Path, FileExtn, "")
If Sheet1.SendRangeFLAG.Value = True Then
Sheet1.Range("A1:I37").ExportAsFixedFormat xlTypePDF, Filename:=Attachment_Path
End If
End If
With myMail.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = ServerPort
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Login_EmailAddress
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Login_EmailPassword
.Update
End With
With myMail
.From = Login_EmailAddress
.Subject = Email_Subject
.To = To_Email
.CC = CC_Email
.BCC = BCC_Email
.TextBody = "Dear soon to be Mr & Mrs" & vbNewLine & _
"" & vbNewLine & _
"Please find attached you latest Installment Receipt for your wedding."
If Attachment_Path <> "" Then .AddAttachment Attachment_Path & FileExtn
End With
On Error Resume Next
myMail.Send
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical
Else
MsgBox ("WOOHOO! IT'S ON IT'S WAY!"), vbInformation
End If
Set myMail = Nothing
End Sub