Access VBA Variables Across PowerPoint Slides
Question details
The user needs to access a VBA variable from procedures associated with different PowerPoint slides.

- Product
- Microsoft PowerPoint
- Device & OS
- not provided
- Scenario
- A public variable named UserName is declared in a slide code module and needed by another procedure.
- Observed behavior
- The procedure cannot access the variable using Slide2.UserName.
Open the presentation in PowerPoint and make sure it contains the slide and text-capable shape you want to update. Save it as a PowerPoint Macro-Enabled Presentation (.pptm) if it is not already macro-enabled.
Declare the shared variable in a standard VBA module
Use a public variable in a standard module so procedures can reference it directly by name.
Slide2 is not normally a VBA object reference, and UserName is not a built-in slide property. Dot syntax accesses an object's properties or methods; it does not make a variable in a slide code module a slide property.
In PowerPoint, open Developer > Visual Basic. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.
In the VBA editor, select the presentation project, then choose Insert > Module.
At the top of the new module, outside any procedure, enter `Public UserName As String`. In a procedure that runs before the value is needed, set it with an assignment such as `UserName = "Alex"`.
In the procedure that needs the value, use `UserName` rather than `Slide2.UserName`. For example, to write it to the second shape on the second slide of the active presentation, use `ActivePresentation.Slides(2).Shapes(2).TextFrame.TextRange.Text = UserName`.
Return to PowerPoint, open Developer > Macros, and run the procedure that sets UserName before running the procedure that displays it. Check the target shape on slide 2.
Edit PowerPoint presentations with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing presentations. Its PowerPoint format support can help you move existing files into your workflow, but it does not fix PowerPoint VBA issues; check macro support before relying on VBA code.

Frequently Asked Questions
Where should I declare a variable shared by PowerPoint macros?
In the VBA editor, choose Insert > Module and declare it at the top of the standard module, outside any procedure, using a statement such as `Public UserName As String`.
Why doesn't Slide2.UserName work?
Slide2 is not normally a VBA object reference, and UserName is not a built-in slide property. Declare the variable in a standard module and reference it directly as `UserName`.
How do I show the variable's value on a slide?
Assign it to a text-capable shape's text range, for example: `ActivePresentation.Slides(2).Shapes(2).TextFrame.TextRange.Text = UserName`. Change the slide and shape references to match your presentation.
Why is my public variable empty when the display macro runs?
A declared string has an empty value until it is assigned. Run the procedure that sets UserName before the procedure that reads or displays it.




