-2

I am Getting error while automating an existing IE window. error is

"Object Variable or with block variable not set" Code is

Sub accessExistingIEBrowser()

      boolWindowFound = False
      Set objShell = CreateObject("Shell.Application")
      WinCount = objShell.Windows.Count
      
       For winNo = 0 To (WinCount - 1)

          strURL = objShell.Windows(winNo).document.Location
          strTitle = objShell.Windows(winNo).document.Title
          If strTitle Like "Sample Form" Then
              Set IE = objShell.Windows(winNo)
              boolWindowFound = True
              Exit For
          Else
          End If

      Next

      If boolWindowFound Then
        Set doc = IE.document
        doc.getElementsByName("fname")(0).Value = "test"
      End If

End Sub
  • You are assuming that every window will have a non-null `document`. That is not the case. – GSerg Jan 10 '21 at 20:09

1 Answers1

-1

"Object Variable or with block variable not set" means that somewhere in your code you omit the Set statement. Set a breakpoint at the beginning of this code (F9), start debugging (F5) and single-step (F8) through your code and you will find where.

StureS
  • 227
  • 2
  • 10
  • Why the downvote? Breakpoints and single-stepping are primary to debugging code successfully. – StureS Jan 11 '21 at 15:19