Discussion:
ActiveSlide and ActivePresentation in VBA, Auto_Open macro
(too old to reply)
Chip Pearson
2006-10-02 02:40:16 UTC
Permalink
For a client, I need to write a small PPT add-in (PPT 2003 SP2). I've never
written a PPT add-in before, but have written hundreds of Excel add-ins. My
questions are as follows:

"ActiveSlide" Does PPT have something like an "ActiveSlide" object that
refers slide that is currently active in PPT? How do you reference in VBA
code the slide that you are currently editing?

"ThisPresentation" Does PPT have something like "ThisPresentation" that
always refers to the presentation in which the running VBA code is located,
regardless of what presentation is active in PPT? In Excel, we have a
ThisWorkbook object that always refers to the workbook containing the code,
regardless of which workbook happens to be active.

If there is no "ThisPresentation" object, or some similar mechanism, how do
you programmatically assign the "OnAction" property of a standard
Office.CommandBarButton. In Excel, I'd use something like
Ctrl.OnAction = ThisWorkbook.Name & "!MacroName"

The "ThisWorkbook" refers to the workbook containing the code, not the
workbook that happens to be open. If there is no such thing, what do you
assign to the OnAction property to ensure it uses the macro in the correct
Presentation.

Finally, is there a way to run a macro whenever that presentation is opened
and closed (in the editor mode, not the slideshow mode)? In Excel, we have
the Auto_Open macro that Excel will automatically execute whenever the
workbook containing it is opened. Similarly, we have Auto_Close that will
run when the workbook is closed. Does PPT VBA have something similar? I need
to run code when my add-in is loaded. I'm starting to think that maybe I
should be writing a COM Add-In in VB6 rather than a PPA add-in.

Thanks in advance for any advice you could give.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)
Bill Dilworth
2006-10-02 04:49:34 UTC
Permalink
Hi Chip,

First, you need to forget 50% of what you know about Excel Macros. The hard
part is knowing which 50%.

PowerPoint uses two modes: edit mode and show mode. The objects are
different and addressing the parts within them are different.

In Edit mode:
ActivePresentation refers to the presentation that has the current
focus.
Windows(1).Selection.SlideRange(1).SlideIndex will get you to the
current slide displayed in edit view

In Show Mode:
Note: Objects in the slideshow can not be selected.
SlideShowWindows(1).View.CurrentShowPosition will get you the slide
being currently displayed

You will need to install an event trap in your add-in to detect the
presentation open event. Try these links...
**How can I get my code to run automatically when a presentation opens?
http://www.pptfaq.com/FAQ00741.htm
- and -
** PowerPoint Add-ins
http://www.pptfaq.com/FAQ00359.htm
--
Bill Dilworth
A proud member of the Microsoft PPT MVP Team
Users helping fellow users.
http://billdilworth.mvps.org
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
vestprog2@ Please read the PowerPoint FAQ pages.
yahoo. They answer most of our questions.
com www.pptfaq.com
.
Post by Chip Pearson
For a client, I need to write a small PPT add-in (PPT 2003 SP2). I've
never written a PPT add-in before, but have written hundreds of Excel
"ActiveSlide" Does PPT have something like an "ActiveSlide" object that
refers slide that is currently active in PPT? How do you reference in VBA
code the slide that you are currently editing?
"ThisPresentation" Does PPT have something like "ThisPresentation" that
always refers to the presentation in which the running VBA code is
located, regardless of what presentation is active in PPT? In Excel, we
have a ThisWorkbook object that always refers to the workbook containing
the code, regardless of which workbook happens to be active.
If there is no "ThisPresentation" object, or some similar mechanism, how
do you programmatically assign the "OnAction" property of a standard
Office.CommandBarButton. In Excel, I'd use something like
Ctrl.OnAction = ThisWorkbook.Name & "!MacroName"
The "ThisWorkbook" refers to the workbook containing the code, not the
workbook that happens to be open. If there is no such thing, what do you
assign to the OnAction property to ensure it uses the macro in the correct
Presentation.
Finally, is there a way to run a macro whenever that presentation is
opened and closed (in the editor mode, not the slideshow mode)? In Excel,
we have the Auto_Open macro that Excel will automatically execute whenever
the workbook containing it is opened. Similarly, we have Auto_Close that
will run when the workbook is closed. Does PPT VBA have something similar?
I need to run code when my add-in is loaded. I'm starting to think that
maybe I should be writing a COM Add-In in VB6 rather than a PPA add-in.
Thanks in advance for any advice you could give.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)
Shyam Pillai
2006-10-02 13:15:36 UTC
Permalink
Hi Chip,

Comments inline.
Post by Chip Pearson
"ActiveSlide" Does PPT have something like an "ActiveSlide" object that
refers slide that is currently active in PPT? How do you reference in VBA
code the slide that you are currently editing?
Nothing native available. Check this out:

Generic function to obtain active slide in any view
http://skp.mvps.org/ppt00044.htm
Post by Chip Pearson
"ThisPresentation" Does PPT have something like "ThisPresentation" that
always refers to the presentation in which the running VBA code is
located, regardless of what presentation is active in PPT? In Excel, we
have a ThisWorkbook object that always refers to the workbook containing
the code, regardless of which workbook happens to be active.
Nope. Create your own reference. Even if you make another presentation
active it will still reference the original one.

Dim ThisPresentation as Presentation
Set ThisPresentation = ActivePresentation
Post by Chip Pearson
If there is no "ThisPresentation" object, or some similar mechanism, how
do you programmatically assign the "OnAction" property of a standard
Office.CommandBarButton. In Excel, I'd use something like
Ctrl.OnAction = ThisWorkbook.Name & "!MacroName"
If it is in the presentation:

Ctrl.OnAction = ThisPresentation.FullName & "!MacroName"

or

Ctrl.OnAction = ActivePresentation.FullName & "!MacroName"

If you want to reference the add-in

Ctrl.OnAction = addins("myaddin.ppa") & "!MacroName"
Post by Chip Pearson
The "ThisWorkbook" refers to the workbook containing the code, not the
workbook that happens to be open. If there is no such thing, what do you
assign to the OnAction property to ensure it uses the macro in the correct
Presentation.
See above.
Post by Chip Pearson
Finally, is there a way to run a macro whenever that presentation is
opened and closed (in the editor mode, not the slideshow mode)? In Excel,
we have the Auto_Open macro that Excel will automatically execute whenever
the workbook containing it is opened. Similarly, we have Auto_Close that
will run when the workbook is closed. Does PPT VBA have something similar?
I need to run code when my add-in is loaded. I'm starting to think that
maybe I should be writing a COM Add-In in VB6 rather than a PPA add-in.
You will get all the PPA information you need here:
PowerPoint add-in FAQ
http://skp.mvps.org/ppafaq.com
--
Regards,
Shyam Pillai

Animation Carbon
http://www.animationcarbon.com
Post by Chip Pearson
For a client, I need to write a small PPT add-in (PPT 2003 SP2). I've
never written a PPT add-in before, but have written hundreds of Excel
Thanks in advance for any advice you could give.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)
Steve Rindsberg
2006-10-02 14:39:49 UTC
Permalink
Post by Chip Pearson
For a client, I need to write a small PPT add-in (PPT 2003 SP2). I've never
written a PPT add-in before, but have written hundreds of Excel add-ins. My
"ActiveSlide" Does PPT have something like an "ActiveSlide" object that
refers slide that is currently active in PPT? How do you reference in VBA
code the slide that you are currently editing?
Dim oSld as Slide
' in some views, more than one slide can be selected,
' so we work with the first item in the range
Set oSld = ActiveWindow.Selection.SlideRange(1).SlideIndex
With oSld
' Do yer stuff
End With
Post by Chip Pearson
"ThisPresentation" Does PPT have something like "ThisPresentation" that
always refers to the presentation in which the running VBA code is located,
regardless of what presentation is active in PPT? In Excel, we have a
ThisWorkbook object that always refers to the workbook containing the code,
regardless of which workbook happens to be active.
If there is no "ThisPresentation" object, or some similar mechanism, how do
you programmatically assign the "OnAction" property of a standard
Office.CommandBarButton. In Excel, I'd use something like
Ctrl.OnAction = ThisWorkbook.Name & "!MacroName"
PPT seems to track the PPT or PPA (addin) that created a given button so all
you need to do is:

Ctrl.OnAction = [name of macro in current PPT/PPA]

But if you do something like this in the add-in's Declarations:

Const MyName = "Addin_Name"

you can write a function that loops through the addins collection looking for
one named Addin_Name and return a reference to it, or any needed info about it.
Post by Chip Pearson
The "ThisWorkbook" refers to the workbook containing the code, not the
workbook that happens to be open. If there is no such thing, what do you
assign to the OnAction property to ensure it uses the macro in the correct
Presentation.
Finally, is there a way to run a macro whenever that presentation is opened
and closed (in the editor mode, not the slideshow mode)?
Whaddaya think this is, Word or something?? ExCEL??? ;-)
PPT is more restrictive than what you're used to. Takes a bit of getting used
to.

Not really. PPT won't run code upon opening a document file. An already
loaded addin can trap presentation open/close events and respond as needed,
though. Auto_Open/Auto_Close subs in an *add-in* will fire as you'd expect
them to though. Keep in mind that add-ins are not the same as PPT files or
templates.

Some of the pages under this heading may help a bit too:

Creating and Installing Add-ins, Toolbars, Buttons
http://www.pptfaq.com/index.html#name_Creating_and_Installing_Add-ins-_Toolbars
-_Buttons_
Post by Chip Pearson
In Excel, we have
the Auto_Open macro that Excel will automatically execute whenever the
workbook containing it is opened. Similarly, we have Auto_Close that will
run when the workbook is closed. Does PPT VBA have something similar? I need
to run code when my add-in is loaded.
I'm starting to think that maybe I
should be writing a COM Add-In in VB6 rather than a PPA add-in.
-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================

Loading...