-
May 29th, 2017, 07:10 AM
#1
Thread Starter
New Member
Save as PDF with default folder and default name
Hi
I am trying to create a VBA code that will save my excel sheet in a pdf format. The difficult part is that I will want a default folder from which I can choose another folder to save the file. Furhtermore, I want the filename to start with Report....
You can see my code here: The VBA code fails in the red-area.
Sub ActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Set wbA = ActiveWorkbook
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = "\\personalfolder\"
'replace spaces and periods in sheet name
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = "Report" & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files(*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
-
May 29th, 2017, 04:24 PM
#2
Re: Save as PDF with default folder and default name
You can see my code here: The VBA code fails in the red-area.
what error occurs?
what version of excel are you using?
is wsA assigned a woksheet object prior to that line in the code?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|