Automating IE - 'Object variable or with block variable not set'

castertroy

New Member
Joined
Mar 6, 2010
Messages
32
I have a code that works fine to take control of an IE window and complete a form. For some reason it isn't working for a specific webpage and is giving the dreaded runtime error 91. Here is the code:

Code:
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 09/03/2011 by X'
Dim Shell As Object
    Dim IE As Object
    Dim oForm As Object
    Dim objShellWins As SHDocVw.ShellWindows
    Dim objIE As SHDocVw.InternetExplorer
    Dim objDoc As Object
    Dim usdURL As String
    Dim myUSD As String
    Dim ws As Worksheet
    Dim UserNameInputBox As HTMLInputElement
Dim IeApp As Object

   Set ws = Worksheets("Sheet1")
    
    Set Shell = CreateObject("Shell.Application")
    
    For Each IE In Shell.Windows
    If IE.LocationURL = "mywebpage" Then
      
            IE.Visible = True
            SetForegroundWindow IE.hwnd
 
Set srch = IE.document.getElementById("strLOGON")
       Set rng = Range("='Sheet1'!A1")
       srch.Value = rng
 
End If
    Next
'
End Sub

I can't figure out why it works for other websites but not this one. Here is the HTML code for the field I am trying to populate.

Code:
        <td align="right" class="normal">Please enter UserID &nbsp<input type="text" onkeypress="return entsub(this.form)" maxlength="16" name="strLOGON" value="" /></td>
    </tr>

Can anyone think of a reason why an error is being generated? This code is tried and tested for 4 similar webpages and I am stumped as to why this one should be different.
 
Thanks for your help!

Your post pointed me in the right direction and I stumbled upon a thread on another website, (coincidentally you had also posted help over there!) where I found a way to do it.


http://www.ozgrid.com/forum/showthread.php?t=74589&page=1

I mixed the code there to assign values to the elements with my existing code to use an existing instance of IE and with a bit of tweaking it worked.

Thanks again!
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Out of interest what was the problem and how did you deal with it?

If it was frames I should probably should have thought of that but the use of frames doesn't seem to be as common as it used to.

Not in the webpages I've seen recently anyway.:)

I think I actually used them in my first ever attempt at webpage, and very smart they were too - at the time.:Eeek:
 
Upvote 0
Here is the basis of the code I ended up using. For some reason this worked for the webpage I was looking at, I think something to do with the use of frames.

Code:
Sub Macro8()
'
' Macro8 Macro
' Macro recorded 23/03/2011 by Gregg '

Dim Shell As Object
    Dim IE As Object
    Dim oForm As Object
    Dim objShellWins As SHDocVw.ShellWindows
    Dim objIE As SHDocVw.InternetExplorer
    Dim objDoc As Object
    Dim usdURL As String
    Dim myUSD As String
    Dim ws As Worksheet
    Dim UserNameInputBox As HTMLInputElement
Dim IeApp As Object
    Dim htmlDoc As MSHTML.IHTMLDocument
    Dim htmlColl As MSHTML.IHTMLElementCollection
    Dim htmlInput As MSHTML.IHTMLInputElement
Dim framed As MSHTML.HTMLDocument
Dim frams As MSHTML.FramesCollection
   Set ws = Worksheets("X")
    
    Set Shell = CreateObject("Shell.Application")
    
    For Each IE In Shell.Windows
    If IE.LocationURL = "XXXX" Then
      
            IE.Visible = True
            SetForegroundWindow IE.hwnd
            
        Do While IE.Busy: DoEvents: Loop
        Do While IE.readyState <> 4: DoEvents: Loop
        
        Set htmlDoc = IE.document
        Set htmlColl = htmlDoc.getElementsByTagName("INPUT")   
      
        ' I have the right document
        Debug.Print "Doc name = " & htmlDoc.Title
        
        
       
                       
                  
                   ' Go through <frame> to get to main document
        Set frmCol = IE.document.frames   'Get the frame collection
        Set htmlDoc = frmCol.Item(0).document   'Get the HTML document within the frame
Set htmlColl = htmlDoc.getElementsByTagName("INPUT")   'Get the input collection from this HTML doc
        For Each htmlInput In htmlColl   'Find the one I want and do stuff
            If htmlInput.Name = "TX50197" Then htmlInput.Value = Range("='X'!B1")
            If htmlInput.Value = Range("='X'!B1") Then htmlInput.Click
        Next htmlInput
 
 
'Handler level
      Set doc = IE.document
        Set frams = doc.frames
        Set framed = frams.Item(0).document 'Get the HTML document within the frame
                
                
        framed.all.Item("DD50198").Value = Range("='Phone Audits'!B2")
 
End If
    Next
'
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top