MS Access VBA to get data from a web browser control's content
Asked Answered
S

1

6

I have successfully used MS Access objects to open a browser window and read the data from that open window. However, I am now trying to read data from a web page that is open inside of a MS Access browser control object inside a form in Access, not in an external browser window.

Based on the code that worked when I was using objects, I tried the following to read data from the content showing inside the browser control in Access:

forms!frmOQWebWindow.webView.Document.getElementByID("lblSessionDate").innerText

and also

forms!frmOQWebWindow.webView.Object.Document.getElementByID("lblSessionDate").innerText

But both of those give me Run-time error '91': "Object variable or With block variable not set"

How can I read the value of an Element with the specified name ("lblSessionDate") when the web page is displayed inside a web browser control inside a MS Access form? I'm sure I'm missing something easy.

In case it helps, here is the code that works when I am using objects to read data from Elements on the same web page that throws an error when I try to read it from a browser control:

Dim weblink As String
Dim objIE As Object
Dim SessionDate As String

Set objIE = CreateObject("InternetExplorer.Application")
weblink = "http://www.somewebpage.com"
objIE.Navigate weblink

While objIE.Busy
    DoEvents
Wend

objIE.Visible = True  'make it visible; set this to false to hide the window

SessionDate = objIE.Document.getElementByID("lblSessionDate").innerText
Sarmentum answered 2/3, 2018 at 23:5 Comment(2)
See https://mcmap.net/q/1777675/-ie-busy-not-working-well-vba/7296893. Use Do While objIE.Busy Or objIE.READYSTATE <> 4. Also, you need to account for things like AJAX depending on the website you're visiting.Apophthegm
Thanks @ErikvonAsmuth. But the web page is completely loaded when check the web browser object in the form, so seems like that is not the problem. I use objIE.Busy for the code that I posted that works for an already open browser window. But, that does not seem to be the problem that is preventing me from checking the content in the web browser control since the web page is completely loaded by the time I'm checking for innerText. The link offered discusses using an IE object outside of a form (which I have already gotten to work) but I need to check a web browser control inside a form.Sarmentum
S
6

Quick Summary: Use the ActiveX web browser control not the MS Access web browser control.

This morning, I solved the problem. I had been very confused why the object browser did not show a .document property for the web browser control that I had added to my Access form. It dawned on me that I had read in another post somewhere out there that the person had gone to great lengths to mention that they had not added a web browser control from the control toolbox on the ribbon bar, but rather had added an ActiveX web browser control.

Once I deleted the Access web browser control and added the ActiveX web browser control instead, things started functioning as expected! The ActiveX browser control had a .document property and I was able to read information from the browser control in my Access form. Here's what I did to get this working:

  1. To insert an ActiveX web browser control into a MS Access Form:
    1. Select "ActiveX Controls" from the Controls dropdown on the MS Access "Form Design Tools"/"Design" tab on the ribbon
    2. Scroll down through the list and select "Microsoft Web Browser" and then click [OK] and place the control onto the form
  2. In the Form_Open event, add the code needed to navigate to the desired web page, for example, with the ActiveX web browser object named webView:

    Me.webView.Object.Navigate "http://www.yoururl.com"
    
  3. To read in data from the web page, I set the form's TimerInterval to 100 (for 100 milliseconds) and put the code there to gather the needed data into variables for use later; once the Timer event triggered, I set the TimerInterval to 0 so that it would not trigger again. For example, in the Form_Timer event and with the ActiveX web browser named webView:

    With Me.webView.Object
        While .Busy Or .ReadyState <> 4  'thanks to @ErikvonAsmuth for .ReadyState <> 4
            DoEvents
        Wend
    
        SDate = .Document.getElementByID("SDate").innerText
        Omega = .Document.getElementByID("Omega").innerText
    
    End With
    
    Me.TimerInterval = 0  'turn off timer since we've got the data needed
    

After the above, I was able to check the values of SDate and Omega in VBA code. Of course, you need to know the ID values of the Elements, but other forums can teach you how to find those ID values.

Hope this helps someone else!

Sarmentum answered 5/3, 2018 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.