I can't seem to figure out why upon GetSaveAsFilename, the file path is not what is coded. If I use the FolderName, the InitialFileName works, but if I tie FileName to the address, it defaults to Documents. The routine also needs to check if the document is open (if open, close it) and/or if the document exists (if true, prompt to overwrite). Any assistance is appreciated.
Code:
Public Sub CreatePDF()
Dim iFile As String
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
username = Environ("username") 'user
FolderName = "C:\Users\" & username & "\Documents\Project\blahblah"
Application.EnableEvents = False
Application.ScreenUpdating = False
On Error GoTo ErrHandler
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") & "_" _
& Replace(ActiveWorkbook.FullName, ".xlsm", "_") _
& "Report_" & Format(Now(), "yyyymmdd\_ampm") & ".pdf"
iFile = FolderName & "\" & strFile
myFile = Application.GetSaveAsFilename(InitialFileName:=iFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Save Report to Directory")
If myFile <> "False" Then
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
MsgBox "PDF file has been created.", vbInformation, ""
End If
ExitHandler:
Exit Sub
Application.EnableEvents = True
Application.ScreenUpdating = True
ErrHandler:
MsgBox "The PDF file could not be created!", vbExclamation, ""
Resume ExitHandler
End Sub