How do I call a VBA subroutine from Javascript?

S

SteveR

I have a VBA form in a PowerPoint add-in that includes a webbrowser control.
In the webbrowser document I've written a Javascript function that, when
called, should pass information to a subroutine in the VBA. How can this be
done?

Thanks,

Steve
 
C

Chirag

From your Javascript code, you can call VBA macro using something like the
following snippet:

<SCRIPT Language = "JScript">
function CallVBA(VBAMacroName)
{
var App;

App = new ActiveXObject("PowerPoint.Application");
App.Run(VBAMacroName);
}
CallVBA("YourVBAMacroNameHere");
</SCRIPT>

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
S

SteveR

Thanks for your reply.

That works for calling a "Macro" but I'd like to call a subroutine within
the macro that's currently running. I create the content of the webbrowser
control (including the javascript) with the macro. Then on a specific
javascript event, I'd like to call a subroutine in the that macro.

Is that possible?

sr
 
S

SteveR

Ok, I tried calling a public sub in my VBA application and I'm getting an
error: "Application.Run: Invalid request, Sub of function not defined."

I tried using App.Run("addQuestion") and
App.Run("addQuestion()") but same error...

function changeIcon() {
App = new ActiveXObject("PowerPoint.Application");
App.Run("addQuestion()");
....

in VBA

public sub addQuestion()
call msgbox("here")
end sub
 
S

SteveR

Steve, thanks again for the reply.

Maybe I wan't clear enough in my opening question. It is an add-in and
it's actually the add-in that creates the content of the webbrowser control
with the Javascript included in it. The add-in displays the userForm and
when the user changes a combobox in the webbrowser control, the combobox's
onChange function should call the subroutine in the add-in. So, the add-in
is definitely running when the Javascript is called.

sr
 
C

Chirag

In that case, you need to qualify the subroutine reference with the add-in
name. If the sub routine name is "addQuestion" and add-in name is "AddIn",
you would need to execute
App.Run("AddIn!addQuestion")
in your code instead of App.Run("addQuestion").

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
S

SteveR

Chirag, That made a lot of sense, but still the exact same error. With
CreateGuide being the name of the add-in, I'm using:

App.Run("CreateGuide!addQuestion") and tried
App.Run("CreateGuide!addQuestion()")

This is PPT 2007, and it feels like it's a completely different product from
everything before it. Could there be some syntax difference in 2007 that
have changed from everything else?

sr
 
S

SteveR

Create the following file and put it in c:\temp.htm (or something)

<html>
<head>
<script>

function test() {
var App = new ActiveXObject('PowerPoint.Application')
App.Run("PPTupload!addQuestion()")
}
</script>
</head>
<body>
<input type="button" value="test" onclick="test()"
</body>
</html>

In the add-in called PPTupload create a subroutine called addQuestion:

public sub addQuestion()
call msgbox("here")
end sub

Add a webbrowser control to the user form and add
webrowser1.navigate2("file://c:\temp.htm")

When you click the button, you should see "here".

sr
 
C

Chirag

Hi,

App.Run("PPTupload!addQuestion()")
should be
App.Run("PPTupload!addQuestion")

The subroutine addQuestion() should be in a standard module (and not a class
module or userform). Are you placing addQuestion() in a standard module?

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
S

SteveR

Very interesting. I had tried it with and without the () but I had the
subroutine in a userform. I put it in the module and it works fine.
Question though, if you can't use the (), how do you pass data from the
Javascript function to the subroutine? I can parse the
document.body.innerHTML but there must be a better way.

Thanks for your input, very helpful.
 
S

SteveR

Perfect! Thanks Chirag and Steve for all your inputs and help. Very much
appreciated.

sr
 

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