none
How do I call a VBA Macro from a WebBrowser control? RRS feed

  • Question

  • I have an Excel workbook which pops up a form that contains a WebBrowser control.  The workbook contains various other VBA macros as well.  When the user clicks on a button on the displayed HTML page I want a JavaScript function to call one of the VBA macros in the workbook.  Can anyone tell me whether this is possible, and if so how to do it?

    I do know how to call back to functions in a VB.NET program from a VB.NET WebBrowser control, using the WebBrowser's ObjectForScripting property and window.external.whatever() in the JavaScript code.  I am assuming there is something analogous going on with the VBA WebBrowser control, but I haven't been able to find it.
    Thursday, October 15, 2009 5:45 PM

Answers

  • Hello,

    We could directly call a macro from JavaScript, please refer to following script:
    <SCRIPT>
    function CallVBA(MacroName)
    {
    var ExApp;

    ExApp = new ActiveXObject("Excel.Application");
    ExApp.Run(MacroName);
    }
    CallVBA("HelloExcel");

    </SCRIPT>

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Thursday, October 22, 2009 6:42 AM
    Wednesday, October 21, 2009 8:14 AM

All replies

  • I have an Excel workbook which pops up a form that contains a WebBrowser control.  The workbook contains various other VBA macros as well.  When the user clicks on a button on the displayed HTML page I want a JavaScript function to call one of the VBA macros in the workbook.  Can anyone tell me whether this is possible, and if so how to do it?

    I do know how to call back to functions in a VB.NET program from a VB.NET WebBrowser control, using the WebBrowser's ObjectForScripting property and window.external.whatever() in the JavaScript code.  I am assuming there is something analogous going on with the VBA WebBrowser control, but I haven't been able to find it.
    • Merged by Martin Xie - MSFT Wednesday, October 21, 2009 7:21 AM Merge it to keep them in the same topic.
    Thursday, October 15, 2009 5:55 AM
  • Probably, but given answers on your question in this forum is abusing the forum.

    Please note that this forum is for VB.NET questions only (e.g. Visual Basic 2002/2003, Visual Basic 2005, Visual Basic 2008, VisualBasic '10).

    For VBA (Visual Basic for Applications), Macros or Excel Questions you will get better support in the VBA Forums:

    Visual Basic for Applications (VBA)
    http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=74&SiteID=1
    (This is a Microsoft MSDN site)

    Alternatively, when developing addins for Office 2003 or 2007 products:

    Visual Studio Tools for Office
    http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16&SiteID=1
    (This is a Microsoft MSDN site)

    For VBScript questions, search Google for a variety of resources or:

    VBScript Forums
    http://www.visualbasicscript.com/
    (A Non-Microsoft Site)


    Success
    Cor
    Thursday, October 15, 2009 8:04 AM
  • Thank you.  I did look for a forum that was VBA specific but somehow I missed that one (I think my eye must have gone right past it because it was in the unfamiliar "Microsoft ISV Community Center" group) , so I assumed this forum was my only choice.  I'll repost my question there.
    Thursday, October 15, 2009 5:34 PM
  • You might also want to delete the thread in this forum, if that can be done.  It popped up as a possible answer when I reposted the question in the VBA forum, which seem undesirable.  :-/
    Thursday, October 15, 2009 5:48 PM
  • Hello,

    We could directly call a macro from JavaScript, please refer to following script:
    <SCRIPT>
    function CallVBA(MacroName)
    {
    var ExApp;

    ExApp = new ActiveXObject("Excel.Application");
    ExApp.Run(MacroName);
    }
    CallVBA("HelloExcel");

    </SCRIPT>

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Thursday, October 22, 2009 6:42 AM
    Wednesday, October 21, 2009 8:14 AM