Fix Excel VBA Saving PDF with Previous Text Box Value
Question details
Users report that when running an Excel VBA macro to export a worksheet as a PDF, dynamic text boxes retain the value from the previous iteration of the loop.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running an automated VBA loop that updates a shape or text box with a new number and sequentially exports the worksheet to PDF format.
- Observed behavior
- The exported PDF reflects the text box value from one iteration prior to the current export step, indicating the graphics engine lags behind the VBA execution speed.
Verify your VBA loop sequence to ensure the variable assignment or text box update occurs completely before the ExportAsFixedFormat command is triggered.
Use DoEvents to Yield Execution and Force Refresh
Implement the DoEvents function to pause macro execution briefly, allowing the operating system to process graphical shape updates before exporting the PDF.
VBA execution often outpaces the application's Graphical Device Interface (GDI). Because text boxes are floating shapes, they take slightly longer to render than standard cell data.
Using DoEvents hands control back to the operating system momentarily, guaranteeing the visual layer catches up with the backend data.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor, and locate your macro code.
Find the specific line of code in your loop where the new value is assigned to the text box shape (e.g., ActiveSheet.Shapes("TextBox1").TextFrame.Characters.Text = newValue).
Immediately after the text box assignment and before the PDF export line, type 'DoEvents' on its own line. You can optionally add it twice ('DoEvents: DoEvents') if the lag is severe.
Execute your macro again to verify that the generated PDFs now display the current iteration's value in the text box.

Toggle ScreenUpdating Before Export
Temporarily re-enable screen updating just before exporting the PDF if your macro turns it off for performance reasons.
Force Worksheet Recalculation
Trigger a manual recalculation if your text box is linked to a cell formula rather than being updated via direct VBA assignment.
Run Macros and Export PDFs Seamlessly with WPS Office
WPS Office offers robust VBA support and an optimized native PDF export engine, ensuring your automated macros execute flawlessly without graphics lag or refresh issues.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your existing macro-enabled workbook (.xlsm).
- 3. Enable Macros: Click 'Enable Macros' if prompted by the security warning at the top of the interface.
- 4. Access the Developer Tab: Navigate to the Developer tab on the ribbon and click 'Macros' to manage your scripts.
- 5. Run Your Script: Execute your PDF automation macro and enjoy perfectly synchronized shape graphics and data.

Frequently Asked Questions
Why does my Excel macro run faster than the screen updates?
Excel's VBA execution engine operates independently of its Graphical Device Interface (GDI). To maximize performance, the system prioritizes code execution over UI redraws, causing visual elements like shapes and text boxes to lag unless explicitly commanded to refresh.
Does disabling screen updating cause PDF export issues?
Yes. If Application.ScreenUpdating is set to False, visual elements like Shapes, Text Boxes, and Charts will not render their updated states. When the ExportAsFixedFormat command fires, it captures the frozen screen state, leading to outdated information in the PDF.
Does this text box delay happen with standard cell values as well?
Rarely. Standard cell values update within the underlying data model almost instantly and are usually captured correctly during PDF export. Floating objects like Text Boxes and Shapes exist on a different rendering layer, making them much more susceptible to visual lag during fast macro loops.




