Using the same IE session from different routine

mnoah

Board Regular
Joined
Oct 14, 2015
Messages
54
Hello,

I have a main sub called 'NameandDOB'. In there I call a sub routine 'ackchecker' which opens IE, goes to a website and checks if there is an acknolwedgment warning. If there isn't, it brings me to the login page. If there is, it checks the box and brings me to the login screen. The sub routine works fine.

The probelm seems that after the 'ackchecker' sub is complete and it goes back to the main sub, it doesn't seem to remember the IE session. I want to use the same IE object that was created in the 'ackchecker' sub routine.

Any help is greatly appreciated!

Code:
Public Sub nameandDOB()Dim objelement As Object
Dim c, LastRow, i, j, intIndex, intMax As Integer
Dim sht As Worksheet
   
    Call ackchecker
    
      With ie.Document
        .all("txtUserName").Value = usertextbox.Value
        .all("txtPassword").Value = passwordtextbox.Value
        .all("btnSubmit").Click
      End With

Code:
Public Sub ackchecker()Set ie = CreateObject("InternetExplorer.Application")


    With ie
        .Visible = True
        .navigate "https://www.njmmis.com"
    End With
    
    On Error Resume Next
    If ie.Document.GetElementByID("chkbx") Is Nothing Then
        'Go to the login page and execute the rest of the procedure
        ie.navigate "https://www.njmmis.com/login.aspx"
    Else
        'Check the box
        ie.Document.all("chkbx").Checked = True
        'Click the continue button
        ie.Document.GetElementsByTagname("input")(1).Click
        ie.navigate "https://www.njmmis.com/login.aspx"
    End If


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I'm guessing that you are not using Object Explicit.
To preserve the object variable ie. it should be declared as a Public variable, outside of any procedure, in a normal Code module

Code:
Public ie as Object

Public Sub ackchecker()
    Set ie = CreateObject("InternetExplorer.Application")


    With ie
        .Visible = True
        .navigate "https://www.njmmis.com"
    End With
    
    On Error Resume Next
    If ie.Document.GetElementByID("chkbx") Is Nothing Then
        'Go to the login page and execute the rest of the procedure
        ie.navigate "https://www.njmmis.com/login.aspx"
    Else
        'Check the box
        ie.Document.all("chkbx").Checked = True
        'Click the continue button
        ie.Document.GetElementsByTagname("input")(1).Click
        ie.navigate "https://www.njmmis.com/login.aspx"
    End If


End Sub
 
Upvote 0
I put 'Public ie as Object' at the very top of the code under 'Option Explicit' but get this error: "Run-time error xxxx:, Method 'Document' of object 'IWebBrowser2' failed" on the line below.


Code:
[COLOR=#333333]Public Sub nameandDOB()
Dim objelement As Object[/COLOR]Dim c, LastRow, i, j, intIndex, intMax As Integer
Dim sht As Worksheet
   
    Call ackchecker
    'Run time error on this line...
      With ie.Document
        .all("txtUserName").Value = usertextbox.Value
        .all("txtPassword").Value = passwordtextbox.Value
        .all("btnSubmit").Click [COLOR=#333333]      
      End With[/COLOR]
 
Upvote 0
You still need to call the macro acChecker.

I have...here is the code in its entirety (irrelevant code omitted)

Code:
Option Explicit
Public ie As Object


Public Sub nameandDOB()
Dim objelement As Object
Dim c, LastRow, i, j, intIndex, intMax As Integer
Dim sht As Worksheet
    Call ackchecker
    
    'Run time error on the next line
      With ie.Document
        .all("txtUserName").Value = usertextbox.Value
        .all("txtPassword").Value = passwordtextbox.Value
        .all("btnSubmit").Click
      End With
    'Rest of the code....
End Sub


Public Sub ackchecker()
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .navigate "https://www.njmmis.com"
    End With
    On Error Resume Next
    If ie.Document.GetElementByID("chkbx") Is Nothing Then
        'Go to the login page and execute the rest of the procedure
        ie.navigate "https://www.njmmis.com/login.aspx"
    Else
        'Check the box
        ie.Document.all("chkbx").Checked = True
        'Click the continue button
        ie.Document.GetElementsByTagname("input")(1).Click
        ie.navigate "https://www.njmmis.com/login.aspx"
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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