Save As Excel & PDF File in Seperate Folder

leewoo5

New Member
Joined
Feb 5, 2017
Messages
13
Hi,

I would like to alter the below code to include saving the file as an excel as well as pdf into the folder...
Additionally, I would like for there to be a hyphen and spaces between the cell values for file name if possible:

Code:
Sub SaveAsPDF()


Dim fName As String
With ActiveSheet
    fName = .Range("L4").Value & .Range("P1").Value & .Range("E6").Value
    .ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
            "I:\FST\Quality\Non-Conformance\Non-Conformance Reports\Open\" & fName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            
            
End With
End Sub

Many thanks,
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If understood. changes in red
Code:
Sub SaveAsPDF()


Dim fName As String
[COLOR=#ff0000]Dim sLoc As String 'location


    sLoc = "I:\FST\Quality\Non-Conformance\Non-Conformance Reports\Open\"[/COLOR]


With ActiveSheet
    fName = .Range("L4").Value & .Range("P1").Value [COLOR=#ff0000]& "-" &[/COLOR] .Range("E6").Value
    
[COLOR=#ff0000]    'Save as xlsx
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=xlOpenXMLWorkbook[/COLOR]
    
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            sLoc & fName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            
            
End With
End Sub
 
Upvote 0
Thanks for your help! Unfortunately there is a compatibility error when saving as an Excel file.
Could the macro run as saving the worksheet as pdf but the workbook as excel? I think this may be the issue?

Many thanks,
 
Upvote 0
Sorry, of course it needs to be macro-enabled so the fileformat should be 'xlOpenXMLWorkbookMacroEnabled':

Code:
[COLOR=#333333]Sub SaveAsPDF()[/COLOR]

Dim fName As String
[COLOR=#ff0000]Dim sLoc As String 'location


    sLoc = "I:\FST\Quality\Non-Conformance\Non-Conformance Reports\Open\"[/COLOR]


With ActiveSheet
    fName = .Range("L4").Value [COLOR=#ff0000]& "-" &[/COLOR]  .Range("P1").Value [COLOR=#ff0000]& "-" &[/COLOR] .Range("E6").Value
    
[COLOR=#ff0000]    'Save as xlsx
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=[/COLOR][COLOR=#ff0000]xlOpenXMLWorkbookMacroEnabled[/COLOR]
    
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            sLoc & fName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            
            
End With [COLOR=#333333]End Sub[/COLOR]

I also missed a hyphon in the name, didn't realise there was 3 parts.
I've tested and it works on my machine.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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