PC Review


Reply
Thread Tools Rate Thread

Can I call an Excel VBA function from within a WebBrowser control?

 
 
John Brock
Guest
Posts: n/a
 
      20th Oct 2009
I have an Excel workbook which can pop up a form that contains a
WebBrowser control. When the user clicks on a button in the web
page that's being displayed in the control, I want a JavaScript
function in the page to call one of the VBA functions in the workbook
and pass it some arguments. Can anyone tell me whether this is
possible, and if so how to do it?

One reason I think it may be possible is that I already know how
to call back to a VB.NET program from a .NET WebBrowser control,
by using the control's ObjectForScripting property and calling
window.external.whatever() in the JavaScript code. It's possible
this is a new feature with .NET, but I am hoping there is some way
to do the same thing from a VBA WebBrowser control, without .NET
being involved.
--
John Brock
(E-Mail Removed)

 
Reply With Quote
 
 
 
 
John Brock
Guest
Posts: n/a
 
      20th Oct 2009
In article <(E-Mail Removed)>,
joel <(E-Mail Removed)> wrote:
>
>Wouldn't the parent of the web browser control be the workbook?


I guess so.

But how do I get access to that parent in the JavaScript code? I
found one link where someone claimed to be doing something like
this (with PowerPoint):

http://www.pcreview.co.uk/forums/thread-3675697.php

But when I adapt for Excel the JavaScript function used in that
link:

function CallVBA(VBAMacroName)
{
var App;

App = new ActiveXObject("Excel.Application");
App.Run(VBAMacroName);
}

what it does is open up a new instance of Excel, which doesn't
help. I need the App variable to point to the already open instance
of Excel that opened up the WebBrowser, but that's what I can't
figure out. It may be that there is a very simple way to do this,
and if so (or even if it's complicated!) I hope someone will
enlighten me.
--
John Brock
(E-Mail Removed)

 
Reply With Quote
 
John Brock
Guest
Posts: n/a
 
      20th Oct 2009
Thanks for the suggestions, but unfortunately they just seem to
throw errors.

Where did you get the "onclick=..." line??? It doesn't seem to be
in the link I posted.

In article <(E-Mail Removed)>,
joel <(E-Mail Removed)> wrote:
>
>I looked at the source code for you posting and found this
>
>
>onclick='javascript:this.parentNode.parentNode.style.display="none";
>
>
>
>I believe if you displayed in Java "this.parentNode.Name" it would be
>the workbook name. If not then try the 2nd parent
>
>"this.parentNode.parentNode.Name"
>
>Once you find the workbook name then the application is what you need
>to run
>
>
>from
>App.Run(VBAMacroName);
>to
>this.parentNode.Application.Run(VBAMacroName);
>
>
>what also may work is
>this.Application.Run(VBAMacroName);
>
>
>I not an expert on Java and not sure how to test my theory.
>
>
>--
>joel
>------------------------------------------------------------------------
>joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
>View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=145854
>



--
John Brock
(E-Mail Removed)

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      21st Oct 2009
I've tried this without success in the past, but your question got me
searching again....

Here's a page explaining how to fire a method in VB from javascript in a
hosted browser control:
http://msdn.microsoft.com/en-us/library/aa752045(VS.85).aspx

It's a bit complex, but easy enough to follow: you can just copy and paste
the code from the link.

One difference in VBA: you can't set the default member of a class via the
Object browser in VBA, so use the method described here:
http://www.cpearson.com/excel/DefaultMember.aspx
It involves exporting the class to a file, then editing and re-importing it.


Worked for me when I tried in XL. I can send you my file if you're having
problems.

Tim

timjwilliams
at
gmail dot com



"John Brock" <(E-Mail Removed)> wrote in message
news:hbkmbp$l9r$(E-Mail Removed)...
> Thanks for the suggestions, but unfortunately they just seem to
> throw errors.
>
> Where did you get the "onclick=..." line??? It doesn't seem to be
> in the link I posted.
>
> In article <(E-Mail Removed)>,
> joel <(E-Mail Removed)> wrote:
>>
>>I looked at the source code for you posting and found this
>>
>>
>>onclick='javascript:this.parentNode.parentNode.style.display="none";
>>
>>
>>
>>I believe if you displayed in Java "this.parentNode.Name" it would be
>>the workbook name. If not then try the 2nd parent
>>
>>"this.parentNode.parentNode.Name"
>>
>>Once you find the workbook name then the application is what you need
>>to run
>>
>>
>>from
>>App.Run(VBAMacroName);
>>to
>>this.parentNode.Application.Run(VBAMacroName);
>>
>>
>>what also may work is
>>this.Application.Run(VBAMacroName);
>>
>>
>>I not an expert on Java and not sure how to test my theory.
>>
>>
>>--
>>joel
>>------------------------------------------------------------------------
>>joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
>>View this thread:
>>http://www.thecodecage.com/forumz/sh...d.php?t=145854
>>

>
>
> --
> John Brock
> (E-Mail Removed)
>



 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      21st Oct 2009
Hi Joel,

I haven't tried it, but if it were possible it would be a pretty big
security problem: once you have a reference to the Excel application
object, you could use it to do all sorts of things...

The links I posted show how to call a method in excel from the web
page, but I'm not sure if there's any support for passing parameters
or returning values or objects to the page. I'll check it out when I
have some time.


Tim


On Oct 21, 3:17*am, joel <joel.40e...@thecodecage.com> wrote:
> Tim : Just for curiosity sake. *Can a Java Script inside the webpage get
> workbook application object that started the web application?
>
> --
> joel
> ------------------------------------------------------------------------
> joel's Profile:http://www.thecodecage.com/forumz/member.php?userid=229
> View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=145854


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I call an Excel VBA function from within a WebBrowser control? John Brock Microsoft Excel Misc 0 20th Oct 2009 02:16 AM
Call a javascript function via vb.net in a webbrowser control Andre Rode Microsoft VB .NET 4 31st Mar 2008 10:22 AM
Thread-safe call to a webbrowser control =?Utf-8?B?Um9iS2lubmV5MQ==?= Microsoft C# .NET 2 11th May 2006 03:25 PM
Re: How to call the print function directly from a webbrowser control Herfried K. Wagner [MVP] Microsoft Dot NET Framework Forms 0 11th Aug 2004 05:55 PM
Call a Script Function from a C# WebBrowser Application Michael Sullivan Microsoft Dot NET 0 22nd Sep 2003 05:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:25 PM.