1

I have a project that I am working on where I am trying to automate a site's behavior via Excel's VBA. So far, I know how to initialize a web browser from VBA, navigate to a website, and perform a simple task such as clicking on an item using the getElementById function and click method. However, I wanted to know how can I go about working with an embedded object(s) that is inside of an iframe.

For example, here is an overview of what the tree structure looks like via HTML source code. Of course, there are a lot more tags, but at least you can get an idea of what it is that I am trying to do.

<html> 
     <body> 
          <div> 
              <iframe class="abs0 hw100" scrolling="no" allowtransparency="true" id="Ifrm1568521068980" src="xxxxx" title="mailboxes - Microsoft Exchange" ldhdlr="1" cf="t" frameborder="0">  <<< ----- This is where I am stuck            
                      <div> 
                         <tbody>  
                              <tr> 
                                  etc..... 
                                  etc.....
                                  etc.....
                              </tr>
                         <tbody>
                    <div> 
----------- close tags 

I guess the biggest problem for me is to learn how to manipulate an embedded object(s) that is enclosed inside of an iframe because all of this is still new to me and I am not an expert in writing programs in VBA. Any help or guidance in the right direction will help me out a lot. Also, if you need more information or clarification, please let me know.

Here the code that I have written so far:

Option Explicit

'Added: Microsoft HTML Object Library
'Added: Microsoft Internet Controls

'Added: Global Variable
Dim URL As String
Dim iE As InternetExplorer

Sub Main()

'Declaring local variables
Dim objCollection As Object
Dim objElement As Object
Dim counterClock As Long

Dim iframeDoc As MSHTML.HTMLDocument 'this variable is from stackoverflow (https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba)

'Predefining URL
URL = ""

'Created InternetExplorer Object
Set iE = CreateObject("InternetExplorer.Application")

'Launching InternetExplorer and navigating to the URL.
With iE
    .Visible = True
    .Navigate URL

    'Waiting for the site to load.
    loadingSite
End With

    'Navigating to the page I need help with that contains the iFrame structure.
    iE.Document.getElementById("Menu_UsersGroups").Click

    'Waiting for the site to load.
    loadingSite

    'Set iframeDoc = iE.frames("iframename").Document '<<-- this is where the error message happens: 438 - object doesn't support this property or method.
    'The iFrame of the page does not have a name. Instead "Ifrm1" is the ID of the iFrame.

End Sub

'Created a function that will be used frequently.

Function loadingSite()

    Application.StatusBar = URL & " is loading. Please wait..."
    Do While iE.Busy = True Or iE.ReadyState <> 4: Debug.Print "loading": Loop
    Application.StatusBar = URL & " Loaded."

End Function

Please note: My knowledge of programming in VBA is on an entry-level. So, please bear with me if I don't understand your answer the first time around. Plus, any nifty documentation or videos about this topic will help me a lot as well. Either way, I'm very determined to learn this language as it is becoming very fun and interesting to me especially when I can get a program to do exactly what it was designed to do. :)

Shadow Slim
  • 23
  • 1
  • 6
  • Most of what you need should be in that linked question (within your code)..... i.e. if same origin policy does not apply you can grab the iframe then .contentDocument.accessorMethodGoesHere....... If you Navigate to the src of the iframe then you just use normal access syntax without contentDocument as you are already in the relevant HTMLDocument. I think the question needs to be made a little bit more specific.... I am trying to do x and y is happening but I wanted z. – QHarr Sep 15 '19 at 07:11
  • 1
    If a webpage has a embedded iframe then it means you have nothing else as a browser inside your browser. The only way i can imagine is to get the URL of this iframe and navigate to THAT URL. Iframes are independend objects. There are some security reasons which also try to block a lot of interaction. – Thomas Ludewig Sep 15 '19 at 20:26

1 Answers1

2

You try to use the following code to get elements from the iframe:

IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click

Sample code as below:

index page:

<input id="txtinput" type="text" /><br />
<input id="btnSayHello" type="button" value="Say Hello" onclick="document.getElementById('result').innerText = 'Hello ' + document.getElementById('txtinput').value" /><br />
<div id="result"></div><br />
<iframe width="500px" height="300px" src="vbaiframecontent.html">

</iframe>

vbaframeContent.html

<input id="txtcontentinput" type="text" /><br />
<input id="btncontentSayHello" type="button" value="Say Hello" onclick="document.getElementById('content_result').innerText = 'Hello ' + document.getElementById('txtcontentinput').value" /><br />
<div id="content_result"></div>

The VBA script as below:

Sub extractTablesData1()
    'we define the essential variables

    Dim IE As Object, Data As Object
    Dim ticket As String


    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate ("<your website url>")

        While IE.ReadyState <> 4
            DoEvents
        Wend

        Set Data = IE.Document.getElementsbyTagName("input")

            'Navigating to the page I need help with that contains the iFrame structure.
        IE.Document.getElementbyId("txtinput").Value = "AAA"
        IE.Document.getElementbyId("btnSayHello").Click


    'Waiting for the site to load.
    'loadingSite

    IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
    IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click



    End With
    Set IE = Nothing
End Sub

After running the script, the result as below:

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30