logo
search
VBA & Macro Problems

Fix VBA Read-Only Property Error When Saving Excel as PDF

Partner EditorPartner Editor Oct 1, 2026 869 views

Question details

The user is attempting to save an Excel invoice as a PDF using a VBA macro but encounters a read-only property error.

How to Fix the VBA Read-Only Property Error When Saving Excel as PDF
Product
Excel
Device & OS
not provided
Scenario
Running a VBA macro script to generate and export an active sheet or invoice as a PDF file.
Observed behavior
The macro execution halts, displaying a "Can't assign to read-only property" error, typically due to incorrect variable assignments, omitted properties, or invalid file paths.
Before you start

Before editing your VBA code, verify that the destination folder on your drive actually exists, you have write permissions to it, and the target PDF filename is not currently open in another application.

Solution 1Recommended

Explicitly Define Values and Concatenate the PDF Extension

Use this solution to correct how VBA reads cell data and constructs the final output file name, avoiding read-only object conflicts.

The "Can't assign to read-only property" error often triggers when you try to assign a value to an object without specifying its property, or when string concatenation for the file path is improperly formatted.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the VBA Editor, and locate the macro script responsible for exporting the PDF.

2
Add the Value Property to Variables

Ensure you are explicitly reading the cell contents by adding '.Value' to your range assignments. For example: InvoiceLetter = Range("G1").Value and InvoiceNumber = Range("H1").Value.

3
Append the PDF Extension

Construct the file name variable by concatenating the parsed values with the '.pdf' string: fName = InvoiceLetter & InvoiceNumber & ".pdf".

4
Update the Export Command

Use the ActiveSheet.ExportAsFixedFormat method and properly concatenate the folder path (sPath) with the filename (fName): ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sPath & fName.

Explicitly Define Values and Concatenate the PDF Extension
Code Accuracy: Adding the .Value property prevents VBA from attempting to overwrite the Range object itself, which is inherently read-only.
Efficient Spreadsheet Automation

Run Macros and Export to PDF Seamlessly with WPS Office

WPS Office provides robust native support for VBA and macros, allowing you to automate tasks like saving invoices to PDF without hassle. Its Spreadsheet tool integrates directly with your existing code.

  1. 1. Open Your Macro File: Launch WPS Spreadsheets and open your .xlsm file containing the invoice macro.
  2. 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'Macros', or press Alt+F11 to open the built-in VBA editor.
  3. 3. Run Your Script: Edit your path or variables as needed, and execute the VBA script to automatically export your active sheets as PDF files.
High compatibility with Microsoft Excel VBA formats (.xlsm)Built-in, error-free PDF export capabilitiesLightweight application that runs smoothly on most desktop devices
microsoft office alternative - wps office

Frequently Asked Questions

Why does VBA throw a 'Can't assign to read-only property' error?

This error occurs when you attempt to change a property that can only be read (not written to), or when you omit a specific property (like .Value) causing VBA to mistakenly try to overwrite the default object structure.

How do I correctly format the file path for ExportAsFixedFormat in VBA?

Ensure your path string includes the drive letter, valid folder names, ends with a backslash (\), and is concatenated properly with the filename and the .pdf extension using the ampersand (&) operator.

Can I save as PDF using VBA without installing third-party add-ins?

Yes, modern spreadsheet applications like Microsoft Excel and WPS Office natively support the ActiveSheet.ExportAsFixedFormat method, allowing direct PDF saving via VBA code without extra plugins.