macro to save file as pdf at a location selected by the User

skf786

Board Regular
Joined
Sep 26, 2010
Messages
156
Hi,

im looking for a macro that saves my file as a PDF and allows user to select the location. File as 4 sheets 1,2,3,4 and the pdf should contain all print areas in the 4 sheets.

thank you in advance.

khalid
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
give user a button to call SavePDF.
note: It may require (In vba, tools, references) ADD REFERENCE : Microsoft Office X.0 Object Library

Code:
Sub SavePDF()
Dim vFile
vFile = UserSave1File()
If vFile <> "" Then ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile
End Sub



  'new way w Office Lib
private Function UserSave1File()
Dim strTable As String
Dim strFilePath As String
Dim sDialogMsg As String, sDecr As String, sExt As String
'Application.FileDialog(msoFileDialogSaveAs) =2
'Application.FileDialog(msoFileDialogFilePicker) =3
With Application.FileDialog(msoFileDialogSaveAs)   'MUST ADD REFERENCE : Microsoft Office X.0 Object Library
.AllowMultiSelect = True
.Title = sDialogMsg ' "Locate a file to Import"
.ButtonName = "Import"
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        If .Show = 0 Then
'There is a problem
Exit Function
        End If
    'Save the first file selected
UserSave1File = Trim(.SelectedItems(1))
End With
End Function
 
Upvote 0
give user a button to call SavePDF.
note: It may require (In vba, tools, references) ADD REFERENCE : Microsoft Office X.0 Object Library

Code:
Sub SavePDF()
Dim vFile
vFile = UserSave1File()
If vFile <> "" Then ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile
End Sub



  'new way w Office Lib
private Function UserSave1File()
Dim strTable As String
Dim strFilePath As String
Dim sDialogMsg As String, sDecr As String, sExt As String
'Application.FileDialog(msoFileDialogSaveAs) =2
'Application.FileDialog(msoFileDialogFilePicker) =3
With Application.FileDialog(msoFileDialogSaveAs)   'MUST ADD REFERENCE : Microsoft Office X.0 Object Library
.AllowMultiSelect = True
.Title = sDialogMsg ' "Locate a file to Import"
.ButtonName = "Import"
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        If .Show = 0 Then
'There is a problem
Exit Function
        End If
    'Save the first file selected
UserSave1File = Trim(.SelectedItems(1))
End With
End Function
thank you so much. It works almost perfectly, except that it saves only the active page. do i need to change the sheet names of the other sheets anywhere in the script?

thanks
 
Upvote 0
have it select all pages before export:
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
 
Upvote 0
have it select all pages before export:
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
thank you this worked well. However after the file is saved, i noticed the excel sheets were all selected in the excel file. How can i deselect in the macro after the pdf is saved?
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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