I want to save as both pdf and xls file when pressing command button?

Solutionmatt

New Member
Joined
Feb 2, 2022
Messages
16
Office Version
  1. 365
  2. 2021
Platform
  1. 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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top