1

I am trying to execute javascript on an IE 11 web page that contains frames and I am using VBA to execute the javascript. This is my first attempt so I am probably making some rookie mistakes. The action I am trying to automate is pressing a "+" sign on a folder to expand it. I have seen a lot of discussion on this topic and I have tried to include what I think is relevant to my code. But after many attempts, I still cannot get it to work. The VBA code I am trying to get to work is as follows. Each call below to execScript has been an attempt to execute the javascript. The VBA error message that I received is placed as a comment before each call. Thanks in advance for any help that you can offer.

    Dim doc             As MSHTML.IHTMLDocument
    Dim Frames          As MSHTML.IHTMLFramesCollection2
    Dim Window          As MSHTML.IHTMLWindow2

    ie.Navigate "URL of my web page"
    Set doc = ie.Document


    ' Error: Method 'frames' of object JScriptTypeInfo failed.

    Call ie.Document.Frames(0).execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

    ' Error: Could not complete the operation due to error 80020101.

    Call ie.Document.Frames.Item(0).execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

    ' Error: Method 'frames' of object JScriptTypeInfo failed.

    Call ie.Document.Frames("navigationBodyFrame").execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

Here is a portion of the HTML for the frame that I was able to grab with Firebug:

    <span id="treeViewRootNode_children" style="display:block">
        <table cellspacing="0" cellpadding="0" border="0">
        <span id="treeViewRootNode_Home_children" style="display:block">
            <table cellspacing="0" cellpadding="0" border="0">
            <span id="treeViewRootNode_Home_MyFolders_children" style="display:none">
            <table cellspacing="0" cellpadding="0" border="0">
                <tbody>
                    <tr>
                        <td>
                        <td>
                            <span id="treeViewRootNode_Home_0_spare"></span>
                            <span id="treeViewRootNode_Home_0_hidden" style="display: none"></span>
                            <a id="treeViewRootNode_Home_0" href="javascript:populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');">
                              <img id="treeViewRootNode_Home_0_lineImg" src="LineImages/lplus.gif" border="0">
                          </a>
                      </td>
                  <td nowrap="true">
              </tr>
          </tbody>
      </table>
      <span id="treeViewRootNode_Home_0_children" style="display:none"></span>
4
  • Why do you need VBA (VB Script?)? Why not pure javascript? Commented Nov 18, 2016 at 3:13
  • Maybe try ie.Document.Frames(0).contentWindow.execScript "alert('ok')" Commented Nov 18, 2016 at 5:04
  • Hi @saille, I am trying to automate a Crystal Reports front end. My plan is post the report into Excel once I automate the report execution. VBA made the most sense to me given I am using Excel. Commented Nov 30, 2016 at 15:35
  • Hi @Tim-Williams, thank you for your suggestion. That line crashes with a "Method 'frames' of object JScriptTypeInfo failed". Commented Nov 30, 2016 at 15:57

1 Answer 1

1

I was able to get this working today. Here is what I did, and there may be an easier way. If there is, I'd be interested in hearing about it.

Using the VBA debugger, I was able to figure out that there were multiple levels of frames in my web page. Using firebug, I was able to figure out which frame contained the icon I wanted to press. Here is the code that pressed the icon I needed to press.

Dim ie             As SHDocVw.InternetExplorer
Dim doc            As MSHTML.HTMLDocument
Dim Frames         As MSHTML.IHTMLFramesCollection2
Dim Window         As MSHTML.IHTMLWindow2

ie.Navigate "URL of my web page"
Set doc = ie.Document
Set Window = doc.Frames(0).Frames(0).Frames(1).Frames(0)
Call Window.execScript("collapseHeader();", "JavaScript")

Set doc = Nothing
Set Window = Nothing
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.