calling a PPT macro from vbscript/wsh

M

mstuehler

All,

I'm working on vbscript (run using windows script host), and I'd like to be
able to call a macro in the Powerpoint document.

There's a property of the Presentation object named VBProject, but I can't
find any documentation on the VBProject object.

So, if I was using this:

Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open("pres.ppt")

How would I call a macro in "pres.ppt" named SomeMacro?

I've tried:

oPres.VBProject.SomeMacro, but that doesn't work.

Any help or guidance is greatly appreciated!

Cheers,
Matt Stuehler
 
M

mstuehler

All,

After a bit of testing, I discovered the following:

Using this code:

Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open("pres.ppt")
oApp.Run "SomeMacro"

I get an error - Powerpoint complains that the macro is undefined

However, if I do this:

Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open("pres.ppt")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "SomeMacro"

Then it works!!!

It's as though Powerpoint doesn't recognize the existence of the macro until
I execute the oPres.Slides(1).Duplicate method

I don't know what other methods have this same effect, but is there a
rational explanation for this?

Many thanks in advance for your advice and insight!

Cheers,
Matt Stuehler
 
M

mstuehler

Steve,

Thanks for your interest! Here's what I've found:

My PPT includes a macro named "someMacro" - for now, it doesn't do anything
except this:

sub someMacro()
MsgBox("Hello")
end sub

My vbscript does this:

Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open("pres.ppt")
oApp.Run "someMacro"

This results in the following error:

Application.Run : Invalid request. Sub or function not defined
Code: 80048240

However, if I change my vbscript to this:

Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open("pres.ppt")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "someMacro"

Then everything works - the vbscript executes without error, and the macro
is called. I can't figure out what "Set oSlide = oPres.Slides(1).Duplicate"
has to do with it, or why it makes a difference, but it does. It's as though
executing this line gives PPT the chance to recognize that someMacro does
exist, but that's just speculation....

Any thoughts or advice are greatly appreciated! I'd like something clear
than adding and removing and unnecessary slide, as that seems like a pretty
lame hack...

Cheers,
Matt Stuehler
 
M

mstuehler

Steve,

EUREKA!!!!!!!!!!!!!!!!!!!

You solved the problem!

I was using this:
oApp.Run "someMacro"

Based on your suggestion, I'm now using this:
oApp.Run "pres.ppt!someMacro"

And it works perfectly!

A huge helping of thanks!

Can you suggest a good source of documentation? I've tried using MSDN's
library, but didn't find this important detail there...

Cheers,
Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top