Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA IE Automation - Read iFrame

Our business uses a browser-based program for operations. I'm automating a solution to navigate through this site, and retrieve some data at the end.

The site itself uses regular frames very heavily. However, at the very end of my process, it populates my data not into a frame, but an iFrame. There's also very extensive javascript throughout the site, making things muddy.

Grabbing the iFrame's src URL and opening in a new browser errors out the page (i.e. the page displays error text instead of the content).

My question:

How can I grab the text out of an iFrame through VBA?

What I've tried so far (feel free to skip):

Target a specific iFrame in a specific frame, and grab innerHTML

With ie.document.frames(myFrameNum).document.getElementsByTagName("iframe")(1).document.body
stringResult = .innerHTML

Target a specific iFrame by ID in a specific frame, and grab innerHTML

Dim iFrm As HTMLIFrame
Set iFrm = ie.document.frames(myFrameNum).document.getElementByID("iFrameID")
Debug.Print iFrm.document.body.innerText

Find any instances of iFrames, and grab them (No results - perhaps because the iframe is embeded in a frame?)

Dim iFrm As HTMLIFrame
Dim doc As HTMLDocument
 For iterator = 0 To ie.document.all.Length - 1
  If TypeName(ie.document.all(iterator)) = "HTMLIFrame" Then
   Set iFrm = ie.document.all(iterator)
   Set doc = iFrm.document
   Debug.Print & doc.body.outerHTML
  End If
Next
like image 415
Aaron Contreras Avatar asked May 22 '13 18:05

Aaron Contreras


People also ask

Can VBA pull data from a website?

You can use VBA to extract data from web pages, either as whole tables or by parsing the underlying HTML elements. This blog shows you how to code both methods (the technique is often called "web-scraping").

Can VBA interact with Internet Explorer?

VBA macro drives internet explorer using its DOM properties and methods. The macro allows to interact with web page controls (text fields and buttons).

Is VBA good for automation?

Although VBA was declared legacy in 2008, this implementation of Visual Basic can help you automate the repetitive tasks in your daily life. The language is object oriented, it's written in C++, and it includes all the features you would expect in a coding language these days.

How do you pull data from a website into Excel using VBA?

Step 1) Open an Excel-based Macro and access the developer option of excel. Step 2) Select Visual Basic option under Developer ribbon. Step 3) Insert a new module. Step 5) Access the reference option under the tool tab and reference Microsoft HTML Object Library and Microsoft internet control.


1 Answers

Try this...

Dim elemCollection As IHTMLElementCollection

Set elemCollection = objDoc.frames("iFrameID").document.all
Debug.Print elemCollection.Item("pagemenuli-adv").innerText
like image 150
Rajesh Avatar answered Sep 19 '22 21:09

Rajesh