0

I'm stumped and I feel like I've got to be missing something dumb...

This macro is intended to call and run a macro whose code is contained in another file and "apply" the macro to the open presentation (from which the initial macro was run). I'm getting a Compile error: Method or data member not found on the second to last line. When I comment that line out, the code runs properly but applies the macro to the wrong presentation.

Any ideas?

Thanks in advance, Joe

Function IsPresentationOpen(Name As String) As Boolean
    Dim codePres As Presentation
    On Error Resume Next
    Set codePres = Application.Presentations.Item(Name)
    IsWorkBookOpen = (Not codePres Is Nothing)

End Function

Sub Run_Macro()

    Dim BriefingTemplate As Presentation
    Set BriefingTemplate = Application.ActivePresentation

'Open Joe's Code Workbook
    Dim xRet As Boolean
    xRet = IsPresentationOpen("CODE.potm")
    If xRet Then
    Else
        Presentations.Open "Direcory\CODE.potm"
    End If

'Run Macro
    BriefingTemplate.Activate                            '<<This is the line w/ the error
    Application.Run ("'CODE.potm'!Macro"), BriefingTemplate


End Sub

Sub a_RunAll_PM(BriefingTemplate As Presentation)

BriefingTemplate.Activate

  Call a_Scorecards_PM
  Call CurrentTemps_PM
  Call RadarSat_PM
  Call Severe_PM
  Call Day1_PM
  Call Day2
  Call JetStream_PM
  Call Operational_Impact_PM
  Call D1_Headlines_PM
  Call D2_Headlines
End Sub

Sub a_Scorecards_PM()
   Dim oPic As Shape
    Set oPic = ActivePresentation.Slides(1).Shapes.AddPicture( _
    FileName:="H:\Weather Briefs\Daily Ops Scorecards\SWA_Page_1.jpg", _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
    Width:=720, Height:=540)

    ActivePresentation.Slides(1).Copy
    ActivePresentation.Slides.Paste 24

End Sub
Joe K
  • 85
  • 1
  • 1
  • 9
  • 1
    FWIW - `IsPresentationOpen` is always returning `False` because you never assign a return value. – YowE3K Dec 10 '17 at 03:27
  • 2
    According to [the documentation](https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/presentation-object-powerpoint), a `Presentation` object does not have an `Activate` method - which is why it is saying that the method does not exist. TBH, I suspect that your problem lies with the code in "Macro", which is probably not written in such a way as to update the correct presentation. So it would probably be useful if you asked us to help you fix that code. – YowE3K Dec 10 '17 at 03:35
  • 1
    simpify your code by getting rid of the `xRet` and using `if not IsPresentationOpen("CODE.potm") then Presentations.Open "Direcory\CODE.potm"` – jsotola Dec 10 '17 at 09:18
  • @YowE3K Ha well the lack of an `Activate` method certainly identifies part of the problem. "Macro" simply adds a bunch of images to specified slides in a presentation, "Run_Macro()" is intended to open the "Code" Presentation and apply that code to the presentation containing "Run_Macro()" (which would've been the "active" window/presentation before . This code has been adapted from a similar system I wrote for Excel VBA. If `Presentation` doesn't have an `Activate`method, how do I switch back to the original presentation in order to apply the macro in "Code.potm"? – Joe K Dec 10 '17 at 17:00
  • @YowE3K Also, how can I fix `IsPresentationOpen`? – Joe K Dec 10 '17 at 17:02
  • 2
    you need at the end a IsPresentationOpen = ..... not IsWorkBookOpen, so as to match the function signature. The signature being Function IsPresentationOpen(Name As String) As Boolean – QHarr Dec 10 '17 at 17:37
  • Ah, gotcha. Thanks @QHarr! – Joe K Dec 10 '17 at 18:51
  • 1
    "how do I switch back to the original presentation in order to apply the macro in "Code.potm"?" The answer is ... you don't. You just need to change `Macro`'s code so that it does its work on whichever presentation you need it to. You are passing the presentation you want to update (i.e. `BriefingTemplate`) to the macro as a parameter - if you show the code for that macro to us we can help you fix it so that it updates the presentation you passed. – YowE3K Dec 10 '17 at 19:05
  • @YowE3K I've updated the code above with the main "Macro", which also calls other subs in the "CODE.potm". Each of those macros pulls a different image in from a specified location. All of those secondary macros (first one copied above) are stable and functional. I have multiple templates that run the same/similar code from "CODE.potm" and am trying to update the system so that I don't have to fix the same thing multiple times every time I want to make changes or adjst the code. – Joe K Dec 10 '17 at 19:55

1 Answers1

1

Rather than "activating" the presentation you want to update, pass the presentation as a parameter to each procedure that needs to access it:

'I am assuming "a_RunAll_PM" is what is called "Macro" in your original question code
Sub a_RunAll_PM(BriefingTemplate As Presentation)

  'Removed "Call" as that is obsolete
  a_Scorecards_PM BriefingTemplate '<-- pass the presentation as a parameter
  CurrentTemps_PM BriefingTemplate '<-- may need to pass to other procedures too?
  RadarSat_PM
  Severe_PM
  Day1_PM
  Day2
  JetStream_PM
  Operational_Impact_PM
  D1_Headlines_PM
  D2_Headlines
End Sub

Sub a_Scorecards_PM(pres as Presentation)
   Dim oPic As Shape
    'Use the "pres" parameter rather than "ActivePresentation"
    Set oPic = pres.Slides(1).Shapes.AddPicture( _
    FileName:="H:\Weather Briefs\Daily Ops Scorecards\SWA_Page_1.jpg", _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
    Width:=720, Height:=540)

    pres.Slides(1).Copy
    pres.Slides.Paste 24

End Sub
YowE3K
  • 23,852
  • 7
  • 26
  • 40
  • This works, but still doesn't solve my issue of applying the macro to the blank open template (what I want) as opposed to the presentation that contains all the code (what it does now since its the most recent, active presentation after opening it to run the code). When I open the template, it opens as Presentation1. Do I try to set BriefingTemplate = Presentation1? – Joe K Dec 10 '17 at 20:29
  • In your question you said `"apply" the macro to the open presentation (from which the initial macro was run)`. If you actually want to apply it to a presentation that wasn't the active presentation when the macro was run then, yes, set a reference to that presentation and pass **that** presentation reference to `a_RunAll_PM`. – YowE3K Dec 10 '17 at 20:41
  • It will be active when the macro is run, however opening the "CODE.potm" automatically makes the "CODE.potm" active instead - thus the macros are "applied" to the "CODE.potm" instead of the desired blank template. How do I set a reference to a presentation without a saved name such as the "Presentation1" that opens when I use a template? – Joe K Dec 10 '17 at 20:53
  • If you are doing an `Open` of a presentation, and you want to use it, set a presentation object to refer to it when you open it - e.g. `Set BriefingTemplate = Presentations.Open(FileName:="C:\My Documents\pres1.pptx", ReadOnly:=msoTrue)`. If you are creating a new presentation, set a presentation object to refer to it when you open it - e.g. `Set BriefingTemplate = Presentations.Add` – YowE3K Dec 10 '17 at 21:02
  • I'm creating a new presentation, but it will already be created by the time the code is run. The code is contained WITHIN the new file (again, opened from a template). I think one of us is missing something, haha. – Joe K Dec 10 '17 at 21:19
  • Yeah, I'm getting very confused - it sounds like the presentation you want to act on is the `ActivePresentation`, so your `Set BriefingTemplate = Application.ActivePresentation` should point to the one you want to use. The fact that you then open the `"CODE.potm"` file won't affect the fact that you have set `BriefingTemplate` to refer to the presentation you want to update. – YowE3K Dec 10 '17 at 21:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160888/discussion-between-joe-k-and-yowe3k). – Joe K Dec 10 '17 at 21:28
  • Setting BriefingTemplate = ActivePresentation is essentially what I had originally. The problem is that when I open the "CODE.potm" file, the "CODE.potm" file then becomes the active presentation. I'd initially simply tried to re-activate the original file (not "CODE.potm", but the file containing the macro that opens "CODE.potm"). That's where the problem lies. – Joe K Dec 10 '17 at 22:28
  • If there's no way to simply Activate it, re-making it = BriefingTemplate, then I'm not sure how to apply the code in "CODE.potm" to the original, blank presentation where the entire process originated from the target of the code in "CODE.potm". Does that help? – Joe K Dec 10 '17 at 22:28
  • `Set BriefingTemplate = Application.ActivePresentation` sets `BriefingTemplate` to refer to whatever presentation was active **at the time the statement was executed**. Changing the active presentation (by opening "CODE.potm") afterwards will not suddenly make `BriefingTemplate` change what it is referring to. – YowE3K Dec 10 '17 at 22:31
  • Are you sure it doesn't set BriefingTemplate to whatever the active presentation is? Otherwise what is causing the images to be inserted into the "CODE.potm" presentation instead of into BriefingTemplate? – Joe K Dec 10 '17 at 23:10
  • Straight after `Sub a_RunAll_PM(BriefingTemplate As Presentation)` place a `MsgBox "BriefingTemplate is " & BriefingTemplate.Name`, and straight after `Set BriefingTemplate = Application.ActivePresentation` (in `Run_Macro`) place a `MsgBox "BriefingTemplate set to " & BriefingTemplate.Name` and see what happens. Hopefully both messages will show the same name. – YowE3K Dec 10 '17 at 23:52