calling a PPT macro from vbscript/wsh

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Mstuehler said:
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

What's the exact text of the errror message, and what's in the macro code?

Or if you can't post that, what if you use something like:

Sub SomeMacro()
MsgBox "SomeMacro reporting for duty, sir!"
End Sub
 
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
 
I'm not sure why duplicating the slide makes it work there, but here's what I've
gotten working (this is w/ PPT2000)

Set oApp = CreateObject("Powerpoint.Application")
' I get an error w/o this
oApp.visible = true
' Name/Path of presentation are different but that shouldn't matter
Set oPres = oApp.Presentations.Open("c:\temp\test.ppt")
' I commented this out
'Set oSlide = oPres.Slides(1).Duplicate
' and made the .Run call specifically point to my file:
oApp.Run "test.ppt!mymacro"

And it woiks!
 
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
 
Steve,

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

I love Eurekas. The bigger and bangier the better. ;-)
Can you suggest a good source of documentation? I've tried using MSDN's
library, but didn't find this important detail there...

Actually, I don't have a clue about VBScript. That was the first one I ever
wrote. But this App.Run "filename!macroname" syntax is how it works in VB/VBA so
I guessed it might work here too.

I'm not sure where it's documented, really. A newsgroup post is probably where i
learned it.

Cheers,
Matt

Steve Rindsberg said:
I'm not sure why duplicating the slide makes it work there, but here's what I've
gotten working (this is w/ PPT2000)

Set oApp = CreateObject("Powerpoint.Application")
' I get an error w/o this
oApp.visible = true
' Name/Path of presentation are different but that shouldn't matter
Set oPres = oApp.Presentations.Open("c:\temp\test.ppt")
' I commented this out
'Set oSlide = oPres.Slides(1).Duplicate
' and made the .Run call specifically point to my file:
oApp.Run "test.ppt!mymacro"

And it woiks!

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

:
 
Back
Top