VBA to prompt to overwrite or save as and attach in email....

lesleyfayburton

New Member
Joined
Dec 4, 2020
Messages
8
Office Version
  1. 2016
Sorry all, I am sure this question has been addressed but I just can't find it. So I want to accomplish (2) things total. and you all have already helped me so much with the first.

1) export excel range to pdf and save it but I need a prompt to ask something like "file already exists do you want to overwrite?" and if "no" "then specify new file name to save as" (I would like to keep the current file name so that I can just add to the end of the name whatever I want to distinguish it from the other. If "yes" then a final message "this ticket has been saved"
2) I want to be able to create and save the ticket and attach it in an email with that file name in the subject of the email. I want the email to open before sending.
So I am thinking I have two macros? One to save a ticket and another one to save and email. so is the best practice to "call" the email macro instead of mashing the two macros together?
Thank you in advance for this tremendous help!

So this is the code I am trying to alter but it takes me down a path that I don't want to go. So I don't even know if I should put this in here but I would like to learn if I could alter this to make it work.

Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

'Check if file already exist - THIS IS WHERE IT GOES SOUTH

If SaveCurrentWorkbook(Dir("T:\Department\Investment Sales\_Restricted\TRADING\TRADE TICKETS\" & Sheets("Trade Ticket").Range("R1").Value & ".pdf")) > 0 Then
xYesorNo = MsgBox(Sheets("Trade Ticket").Range("R1") & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorN = vbYes Then
Kill xFolder
Else

MsgBox "if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, FileName:=xFolder, Quality:=xlQualityStandard

'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display

.To = ""
.CC = "" & ""
.Subject = FileName
.Attachments.Add PathFileName
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Sorry all, I am sure this question has been addressed but I just can't find it. So I want to accomplish (2) things total. and you all have already helped me so much with the first.

1) export excel range to pdf and save it but I need a prompt to ask something like "file already exists do you want to overwrite?" and if "no" "then specify new file name to save as" (I would like to keep the current file name so that I can just add to the end of the name whatever I want to distinguish it from the other. If "yes" then a final message "this ticket has been saved"
2) I want to be able to create and save the ticket and attach it in an email with that file name in the subject of the email. I want the email to open before sending.
So I am thinking I have two macros? One to save a ticket and another one to save and email. so is the best practice to "call" the email macro instead of mashing the two macros together?
Thank you in advance for this tremendous help!

So this is the code I am trying to alter but it takes me down a path that I don't want to go. So I don't even know if I should put this in here but I would like to learn if I could alter this to make it work.

Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

'Check if file already exist - THIS IS WHERE IT GOES SOUTH

If SaveCurrentWorkbook(Dir("T:\Department\Investment Sales\_Restricted\TRADING\TRADE TICKETS\" & Sheets("Trade Ticket").Range("R1").Value & ".pdf")) > 0 Then
xYesorNo = MsgBox(Sheets("Trade Ticket").Range("R1") & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorN = vbYes Then
Kill xFolder
Else

MsgBox "if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, FileName:=xFolder, Quality:=xlQualityStandard

'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display

.To = ""
.CC = "" & ""
.Subject = FileName
.Attachments.Add PathFileName
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
bump
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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