Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run VBA after Presentation in PowerPoint Opens

Status
Not open for further replies.

DBDivaAuto

IS-IT--Management
May 9, 2017
29
US
It's been a while since I have had to code something in PowerPoint (as in over 4 years) and things have changed a bit. I would like a piece of code to run when the presentation is done opening.

We have slides that get uploaded to a file documentation system and when they do, it changes whatever the file name is to the Document Number and Version. If someone downloads to their machine, we want to replace the Rev # with the document title so it's all consistent. I can run the OnLoadCode manually and it does what I expect. How in Powerpoint can this happen with a mouse motion, on load, etc?

Any help is appreciated.



Code:
Public App As Application
Private Sub App_AfterPresentationOpen(ByVal Pres As Presentation)
    With Pres
    
        OnLoadCode
    
    End With


End Sub


'Sub Auto_open()
 '   OnLoadCode
'End Sub

Sub OnLoadCode()
    Dim sld As Slide
    Dim shp As Shape
    Dim fileName As String
    Set sld = ActivePresentation.Slides(1)
    
    fileName = Replace(Application.ActivePresentation.Name, ".pptm", "")
    
    
    'MsgBox fileName
    
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then
                shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "1", fileName)
            End If
        End If
    Next shp
End Sub
 
First, you need to make powerpoint to run initial macro. This can be done in powerpoint add-in with Auto_Open procedure, it does not work for regular presentations. This add-in has to have class module that has a public application variable declared with WithEvents and selected application events procedure. Auto-Open procedure should instantiate the class and assign application to the variable declared with WithEvents.

Having this, you will pick all presentations that powerpoint opena, as long as the add-in is installed

combo
 
I have never had to make an addin - I tried yesterday for about 3 hours and had no luck. Any hints, clues, help on how to do this. BTW I have Microsoft 365.
 
You have to save presentation as powerpoint add-in type (.ppam). Powerpoint suggests folder for add-ins. Add-ins can be open or not when powerpoint starts, this is set either in options settings or in developer tab, both for powerpoint add-ins type.

combo
 
I have it saved as a .ppam and it is going to an AppData folder on the C: but it is not running any code on open.
 
Do you have Auto_Open procedure in the add-in? Is the add-in turned on? Try to insert MsgBox in the procedure, you will be sure that the code runs.

combo
 
I can insert a button and run the code no problem. It's just trying to get an add in to work. I don't think this can be a permanent solution - I'd have to install the stupid add in on every machine in order to make it run. Sometimes Microsoft just gets it wrong.
 
There are workarounds ... whether you consider them more or less convoluted than creating and distributing an add-in is going to be down to personal preference, however

For example, with a little cunning, you can respond to the presentations Ribbon opening (assuming you are using a version of Powerpoint with the Ribbon interface), by adding some custonmUI XML (which needs an offcie custom UI XML ediror; the one I use is found here, but there are others, including an MS version

All it takes is

1 Add a module to your presentation
2 Add the following code:

Code:
[COLOR=blue]Option Explicit

Public Sub onLoadRibbon(myRibbon As IRibbonUI)
  [COLOR=green]' Add your code here[/color]
  Example [COLOR=green]' just an example[/color]
End Sub

Public Sub Example()
    MsgBox ActivePresentation.Name & " has " & ActivePresentation.Slides.Count & " slides"
End Sub[/color]

3. Save and close your presentation
4. Open presentation in Custom UI editor
5. Add the following highly complex UI customisation:

Code:
<customUI xmlns="[URL unfurl="true"]http://schemas.microsoft.com/office/2006/01/customui"[/URL]
onLoad="onLoadRibbon" ></customUI>

6. Save it.

All done

Opening your presentation in Powerpoint will trigger the UI's [tt]OnLoad[/tt] event, which in turn now triggers your [tt]onLoadRibbon[/tt] code
 
Finally - easier that I thought. I was able to make the add-in and auto run - we will just have to have 2 engineers run. Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top