Save to specific folder

Willsy3369

New Member
Joined
Aug 3, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi all, I am using this code on spreadsheet and I am trying to save this to a specific folder on my drive. Can someone give me assistance to point me in the right direction. Below is the coding that I'm using.

Sub PDFActiveSheetNoPrompt()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " - " & wsA.Range("A3").Value

strFile = strName & ".pdf"
strPathFile = strPath & strFile

'export to PDF in current folder
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& strPathFile

exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Welcome to the Board!

This section of your code:
VBA Code:
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
is grabbing the current path of your active workbook, and using that as the path to save the file to.

If you want to save it to some other specific path, replace that whole block of code with something like this:
VBA Code:
strPath = "C:\your path here\"
where you obviously enter in the path you want to save the files to.
 
Upvote 0
Welcome to the Board!

This section of your code:
VBA Code:
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
is grabbing the current path of your active workbook, and using that as the path to save the file to.

If you want to save it to some other specific path, replace that whole block of code with something like this:
VBA Code:
strPath = "C:\your path here\"
where you obviously enter in the path you want to save the files to.
Hi,
I have made those changes and now I get this message
1659668443394.png
 
Upvote 0
We need to see what that error is, and unfortunately, you suppressed it in your code.
Change this line:
VBA Code:
MsgBox "Could not create PDF file"
to this:
VBA Code:
MsgBox err.Number & ": " & err.Description
and tell us what it says.
 
Upvote 0
Where did you get this code?
Has it worked before?

The beginning of this line does not look correct to me:
Rich (BB code):
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
I don't understand the "ExportAsFixedFormat" if you are trying to save to a PDF.
 
Upvote 0
Where did you get this code?
Has it worked before?

The beginning of this line does not look correct to me:
Rich (BB code):
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
I don't understand the "ExportAsFixedFormat" if you are trying to save to a PDF.I got this
 
Upvote 0
I got this code from Excel Macro to Save Sheets As PDF. The code does work and it saves into the folder where the spreadsheet is located. I was seeing if there was a way to export/save the sheet to specific folder.
When I revert back to the original code for the destination save folder strPath = strPath & "\" (which is also the same folder where the file is, I get the file saved as pdf.
1659752038398.png

All I'm trying to do is to save this formatted pdf into a specific folder that no one else has access to.
 
Upvote 0
Is that specific folder a OneDrive or Sharepoint folder by any chance ?
If not before the ExportAs line put this line.
VBA Code:
debug.print strPathFile

Check if what you see in the immediate window makes sense. (Ctrl+G in VBA editor if you can't see it)
If you think it does make sense try to export in manually and copy in that full path-filename and see if it works.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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