jeepster0000
New Member
- Joined
- Jan 22, 2019
- Messages
- 5
I am running an excel macro that saves it as a PDF. I want it to pop up with a default file name and ask the user if they want to overwrite it. If they say yes, then overwrite, but if they say no, then they should be prompted to change the file name. It does this, but if the user selects save again, then the file saves automatically without confirming if they want to overwrite it.
It works almost like it is supposed to, but It is doing something weird.
If you click save as, the pdf save as pops up with the default file name GOOD
If you try to overwrite the file a message box appears to verify you want to overwrite GOOD
If you click no to over write, but click save again, the message box does not appear, and it overwrites the file anyways. BAD
I have taken out all loops I was trying so it would not jumble the code up for you.
Here is a copy of the code that is whooping my butt. If you have a sec, could you take a look? I owe you big
It works almost like it is supposed to, but It is doing something weird.
If you click save as, the pdf save as pops up with the default file name GOOD
If you try to overwrite the file a message box appears to verify you want to overwrite GOOD
If you click no to over write, but click save again, the message box does not appear, and it overwrites the file anyways. BAD
I have taken out all loops I was trying so it would not jumble the code up for you.
Here is a copy of the code that is whooping my butt. If you have a sec, could you take a look? I owe you big
Code:
[COLOR=#333333]Sub ExportPDFCheckFile()[/COLOR]
[COLOR=#333333]'contextures dot com for Excel 2010 and later[/COLOR]
[COLOR=#333333]' Code taken from [/COLOR]
[COLOR=#333333]'checks for existing file[/COLOR]
[COLOR=#333333]'prompt to overwrite or rename[/COLOR]
[COLOR=#333333]'uses bFileExists Function, below[/COLOR]
[COLOR=#333333]Dim wsA As Worksheet[/COLOR]
[COLOR=#333333]Dim wbA As Workbook[/COLOR]
[COLOR=#333333]Dim strTime As String[/COLOR]
[COLOR=#333333]Dim strName As String[/COLOR]
[COLOR=#333333]Dim strPath As String[/COLOR]
[COLOR=#333333]Dim strFile As String[/COLOR]
[COLOR=#333333]Dim strPathFile As String[/COLOR]
[COLOR=#333333]Dim strOWcheck As String[/COLOR]
[COLOR=#333333]Dim myFile As Variant[/COLOR]
[COLOR=#333333]Dim lOver As Long[/COLOR]
[COLOR=#333333]On Error GoTo errHandler[/COLOR]
[COLOR=#333333]'Activate Worksheet to save as pdf[/COLOR]
[COLOR=#333333]Sheets("PrintForm").Activate[/COLOR]
[COLOR=#333333]Set wbA = ActiveWorkbook[/COLOR]
[COLOR=#333333]Set wsA = ActiveSheet[/COLOR]
[COLOR=#333333]'Uses time stamp for file name[/COLOR]
[COLOR=#333333]strTime = Format(Now(), "yyyy_mm_dd")[/COLOR]
[COLOR=#333333]'get active workbook folder, if saved[/COLOR]
[COLOR=#333333]strPath = wbA.Path[/COLOR]
[COLOR=#333333]'Puts \ in path[/COLOR]
[COLOR=#333333]If strPath = "" Then[/COLOR]
[COLOR=#333333]strPath = Application.DefaultFilePath[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]strPath = strPath & ""[/COLOR]
[COLOR=#333333]'create default name for savng file[/COLOR]
[COLOR=#333333]strName = Sheets("LockedData").Range("F6").Text[/COLOR]
[COLOR=#333333]strName = Replace(strName, " ", "")[/COLOR]
[COLOR=#333333]strName = Replace(strName, ".", "_")[/COLOR]
[COLOR=#333333]strName = Replace(strName, "/", "")[/COLOR]
[COLOR=#333333]strName = Replace(strName, "&", "-")[/COLOR]
[COLOR=#333333]'create default path for saving[/COLOR]
[COLOR=#333333]strFile = strTime & strName & ".pdf"[/COLOR]
[COLOR=#333333]strPathFile = strPath & strFile[/COLOR]
[COLOR=#333333]'Test if file exsts[/COLOR]
[COLOR=#333333]If bFileExists(strPathFile) Then[/COLOR]
[COLOR=#333333]myFile = Application.GetSaveAsFilename _[/COLOR]
[COLOR=#333333](InitialFileName:=strPathFile, _[/COLOR]
[COLOR=#333333]FileFilter:="PDF Files (*.pdf), *.pdf", _[/COLOR]
[COLOR=#333333]Title:="Select Folder and FileName to save")[/COLOR]
[COLOR=#333333]'pop up box for overwright[/COLOR]
[COLOR=#333333]lOver = MsgBox("Overwrite existing file?", _[/COLOR]
[COLOR=#333333]vbQuestion + vbYesNo, "File Exists")[/COLOR]
[COLOR=#333333]strOWcheck = lOver[/COLOR]
[COLOR=#333333]If lOver <> vbYes Then[/COLOR]
[COLOR=#333333]'user can enter name and[/COLOR]
[COLOR=#333333]' select folder for file[/COLOR]
[COLOR=#333333]myFile = Application.GetSaveAsFilename _[/COLOR]
[COLOR=#333333](InitialFileName:=strPathFile, _[/COLOR]
[COLOR=#333333]FileFilter:="PDF Files (*.pdf), *.pdf", _[/COLOR]
[COLOR=#333333]Title:="Select Folder and FileName to save")[/COLOR]
[COLOR=#333333]If myFile <> "False" Then[/COLOR]
[COLOR=#333333]wsA.ExportAsFixedFormat _[/COLOR]
[COLOR=#333333]Type:=xlTypePDF, _[/COLOR]
[COLOR=#333333]Filename:=myFile, _[/COLOR]
[COLOR=#333333]Quality:=xlQualityStandard, _[/COLOR]
[COLOR=#333333]IncludeDocProperties:=True, _[/COLOR]
[COLOR=#333333]IgnorePrintAreas:=False, _[/COLOR]
[COLOR=#333333]OpenAfterPublish:=False[/COLOR]
[COLOR=#333333]'confirmation message with file info[/COLOR]
[COLOR=#333333]'Activate Worksheet that button was on[/COLOR]
[COLOR=#333333]Sheets("Fill").Activate[/COLOR]
[COLOR=#333333]MsgBox "PDF file has been created: " _[/COLOR]
[COLOR=#333333]& vbCrLf _[/COLOR]
[COLOR=#333333]& myFile[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]GoTo exitHandler[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]'export to PDF in current folder[/COLOR]
[COLOR=#333333]wsA.ExportAsFixedFormat _[/COLOR]
[COLOR=#333333]Type:=xlTypePDF, _[/COLOR]
[COLOR=#333333]Filename:=strPathFile, _[/COLOR]
[COLOR=#333333]Quality:=xlQualityStandard, _[/COLOR]
[COLOR=#333333]IncludeDocProperties:=True, _[/COLOR]
[COLOR=#333333]IgnorePrintAreas:=False, _[/COLOR]
[COLOR=#333333]OpenAfterPublish:=False[/COLOR]
[COLOR=#333333]'confirmation message with file info[/COLOR]
[COLOR=#333333]MsgBox "PDF file has been created: " _[/COLOR]
[COLOR=#333333]& vbCrLf _[/COLOR]
[COLOR=#333333]& strPathFile[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]exitHandler:[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]
[COLOR=#333333]errHandler:[/COLOR]
[COLOR=#333333]'Error Message, that File was not created[/COLOR]
[COLOR=#333333]MsgBox "Could not create PDF file"[/COLOR]
[COLOR=#333333]Resume exitHandler[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]'=============================[/COLOR]
[COLOR=#333333]Function bFileExists(rsFullPath As String) As Boolean[/COLOR]
[COLOR=#333333]bFileExists = CBool(Len(Dir$(rsFullPath)) > 0)[/COLOR]
[COLOR=#333333]End Function[/COLOR]
Last edited by a moderator: