PowerPoint vba BeforeSaveAs

516 Views Asked by At

I have a PowerPoint template, which is links up with Excel. Some of the areas in Excel has been copied with links, so that it will automatically update.

Whenever this PowerPoint template will be Saved As, I need to remove these links to external Excel Workbooks.

Is there somehow to do this in PowerPoint just like

Private Sub Workbook_Before Save(ByVal SaveAsUI As Boolean, Cancel As Boolean) in Excel?

So far

I tried the below-mentioned answer, without any luck. The code somehow seems to not run - here I don't know if I'm doing it wrong. I tried running it in a normal module and a class module - without any way of provoking it to happen. Then I tried running it as a normal sub, and here I got errors on the HasRevisionInfoand alsoApplication.PresentationBeforeSave.

2

There are 2 best solutions below

3
On

Yes there is, look into Application.PresentationBeforeSave event which Occurs before a presentation is saved. Here is vb example

Private Sub PPTApp_PresentationBeforeSave(ByVal Pres As Presentation, _
        Cancel As Boolean)

    Dim intResponse As Integer

    Set Pres = ActivePresentation

    If Pres.HasRevisionInfo Then

        intResponse = MsgBox(Prompt:="The presentation contains revisions. " & _
            "Do you want to accept the revisions before saving?", Buttons:=vbYesNo)

        If intResponse = vbYes Then

            Cancel = True

            MsgBox "Your presentation was not saved."

        End If

    End If

End Sub
0
On

I got it to work after a lot of research, @0m3R provided me with some of the right answer.

Somehow I found somewhere, that I had to combine a class module with a regular module.

Here's the code for the Class Module:

Private Sub PPTApp_PresentationBeforeSave(ByVal Pres As Presentation, Cancel As Boolean)
Dim sld As Slide
Dim shp As Shape
Dim TextValue As String
Dim intResponse As Integer

Set Pres = ActivePresentation

TextValue = "You're about to save this PowerPoint." & Chr(10) & "This Powerpoint is programmed to break all links" & _
" meaning that all of the content will not be updated automatically anymore." & Chr(10) & Chr(10) & _
"Do you wish to break all links?"

If Pres.Name <> "A3.potm" Then

intResponse = MsgBox(TextValue, Buttons:=vbYesNo)

If intResponse = vbYes Then
    For Each sld In Pres.Slides
        For Each shp In sld.Shapes
            On Error Resume Next
                shp.LinkFormat.BreakLink
            On Error GoTo 0
        Next shp
    Next sld
Else
MsgBox "You didn't break all links - the presentation may be overwritten in the future..."
End If
End If
End Sub

Here's the code for the regular Module

Option Explicit
Dim cPPTObject As New cEventClass

Sub InitializeApp()
    Set cPPTObject.PPTApp = Application
End Sub

I chose to make a "Command Button" in my PowerPoint, to have the user run a code before viewing the presentation. Then whenever they will save this presentation, the have to choose if they want to delete the links or not :)

Thank you for your assistance :)