How to Save Excel PDFs Directly to a SharePoint Folder Using VBA
Question details
The user wants to utilize VBA macros to export Excel files as PDFs and save them directly into a SharePoint folder, bypassing the need to save them manually to a local drive first.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the generation of PDF reports from Excel worksheets and uploading them directly to a shared SharePoint document library.
- Observed behavior
- By default, VBA macros using the Print to PDF or export functions require a local file path, meaning users normally have to save the PDF locally and then manually upload it to SharePoint.
Ensure you have the necessary permissions to access the target SharePoint site and that you have the Microsoft OneDrive sync client installed and running on your computer.
Synchronize SharePoint and Use a Local VBA Path
The most reliable method to save PDFs to SharePoint via VBA is to sync the SharePoint library to your local computer, allowing the macro to save the file to a standard directory that automatically uploads to the cloud.
VBA's native PDF export function works best with local directory paths rather than direct HTTP SharePoint URLs. By using OneDrive to sync your SharePoint document library to File Explorer, Excel can treat the cloud folder like a regular local drive.
When the VBA macro saves the PDF into this synchronized folder, the OneDrive client instantly detects the new file and uploads it to SharePoint in the background without any extra coding required.
Navigate to your target SharePoint document library in a web browser. Click the 'Sync' button in the top menu to link the folder with your local OneDrive client.
Open Windows File Explorer and find the newly synced SharePoint folder (usually listed under your organization's name in the left navigation pane). Right-click the folder, select 'Properties', and copy the folder's local location path (e.g., C:\Users\username\contoso\site-documentlibrary\folder\).
Open your Excel workbook and press ALT + F11 to launch the VBA Editor. Insert a new Module by clicking 'Insert' > 'Module' from the top menu.
Paste the following code, replacing the saveLocation path with your synced folder path: Sub SaveActiveSheetsAsPDF() Dim saveLocation As String Dim filename As String filename = Range("C3").Value saveLocation = "C:\Users\username\contoso\site-documentlibrary\folder\" & filename & ".pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation End Sub
Press F5 or click the Run button to execute the macro. The active sheet will be saved as a PDF in the synced folder and automatically uploaded to SharePoint.

Automate PDF Conversion using Power Automate Desktop
If you prefer a low-code approach instead of VBA, you can use Power Automate Desktop to extract the Excel data and save it directly as a PDF to your synchronized folder.
Run VBA Macros to Save PDFs in WPS Office
WPS Spreadsheets fully supports VBA macros and features native PDF exporting. You can use the exact same VBA code in WPS Office to automate saving your worksheets as PDFs directly to your synced SharePoint folders.
- 1. Install WPS Office: Download and install WPS Office, ensuring you select the version that includes VBA support during or after installation.
- 2. Open Your Workbook: Launch WPS Spreadsheets, open your document, and press ALT + F11 to open the built-in VBA Editor.
- 3. Run the Macro: Paste your PDF export macro script, update the path to your synced SharePoint directory, and run the code to generate the PDF.

Frequently Asked Questions
Why do I get a path error when saving directly to a SharePoint URL using VBA?
The VBA ExportAsFixedFormat method relies on the local Windows file system. If you input a direct HTTP/HTTPS SharePoint URL, VBA cannot process the path correctly. You must sync the SharePoint folder via OneDrive first and use the local C:\ drive path.
Can I name the exported PDF based on a specific cell value?
Yes. You can declare a variable in your VBA code (e.g., filename = Range("C3").Value) and append it to your folder path string (saveLocation = "C:\path\" & filename & ".pdf") before executing the save command.
Does the VBA macro wait for the SharePoint upload to finish before completing?
No. The VBA macro simply saves the PDF to the local synchronized folder and finishes its execution immediately. The OneDrive sync client operates independently in the background to handle the actual upload to SharePoint.
Will this VBA method work on Mac OS?
The specific file paths (using C:\) and the OneDrive synchronization client behave differently on macOS. While you can sync SharePoint to a Mac, you will need to format the saveLocation path string using Mac-specific folder paths (e.g., using slashes / instead of backslashes \).




