Link to home
Start Free TrialLog in
Avatar of KentDBerry
KentDBerry

asked on

Without using SendKeys, How may I automate Internet Explorer 6 items in its Menu bar using VB 6 or VBA?

I have an Excel Application with a column having hyperlinked data.  The hyperlink automatically opens up a webpage and then I want to automate finding the text that was in the cell that I clicked on.  Using SendKeys has been giving me intermittent results, so I want to tap into internet explorer's object model.

So without using SendKeys, How may I automate Internet Explorer 6 items in its Menu bar using VB 6 or VBA?
Avatar of wobbled
wobbled
Flag of United Kingdom of Great Britain and Northern Ireland image

You can manipulate internet explorer via VB.  Have a look at

http://msdn2.microsoft.com/en-us/library/aa752084(VS.85).aspx

This will give you an over view.  There is a menubar property that you may be able to manipulate
Avatar of KentDBerry
KentDBerry

ASKER

Thanks.  I've seen this page.  Unfortunately, I need a "method" rather than a property to be able to execute the "Find on this page" menu item.  

I'm finding info about the explorer archetecture and it appears that I want to be able to tap into the BrowsUI.DLL (or is it BrowseUI.dll) which is the user interface code for internet exlorer.

Any one know how to tap into that library?

Avatar of Saurabh Singh Teotia
As far as i have used VB with Internetexplorer...You can tap in only by send keys method only..otherwise there is no way by which you can sent information to IE Through VB.......
ASKER CERTIFIED SOLUTION
Avatar of david251
david251

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Again.  Perfect.  Thanks so much
It took me a couple of days but I finally got it.
Glad I could help:-)
Thanks for the points/grade.
-David251
Yes.  Thanks for hanging in there.  One followup question.  

For subsequent user clicks, I desire to keep the same browser open.  so I modified as shown below.  Note that I  am dimming ie as a global and do not close the browser via code so that user can click on a second protocol hyperlink.

However, if the user closes the browser it will make the code bomb.  How would you suggest I handle that scenario?


Dim ie As InternetExplorer
 
Public Function ieFindAndScroll(strCurrentProtocolName As String)
    On Error GoTo endit
    
    Dim bFound As Boolean
 
    If ie Is Nothing Then
        Set ie = New InternetExplorer
        ie.Navigate "http://nafta1.pro.intra/brd/TT/FieldCrops/" & Mid(Right(ActiveWorkbook.Name, 8), 1, 4) & "/Protocols/SortedByProtocol_Study.html"
        Do Until ie.ReadyState = READYSTATE_COMPLETE
        Loop
    End If
 
    Set txt = ie.Document.body.createTextRange()
    bFound = txt.findText(strCurrentProtocolName)
    If bFound = True Then
        ie.Visible = True
        txt.MoveStart "character", -1
        txt.findText strCurrentProtocolName
        txt.Select
        txt.ScrollIntoView
    Else
        MsgBox "Protocol " & strCurrentProtocolName & " was not found on the BRD website!", vbInformation + vbOKOnly
    End If
    
    Exit Function
endit:
     MsgBox Err.Number & Err.Description
    
End Function

Open in new window

I figured it out as posted below:
Public ie As InternetExplorer
 
Public Function ieFindAndScroll(strCurrentProtocolName As String)
    On Error GoTo endit
    
    Dim bFound As Boolean
beginning:
    If ie Is Nothing Then
        Set ie = New InternetExplorer
        ie.Navigate "http://nafta1.pro.intra/brd/TT/FieldCrops/" & Mid(Right(ActiveWorkbook.Name, 8), 1, 4) & "/Protocols/SortedByProtocol_Study.html"
        Do Until ie.ReadyState = READYSTATE_COMPLETE
        Loop
    End If
 
    Set txt = ie.Document.body.createTextRange()
    bFound = txt.findText(strCurrentProtocolName)
    If bFound = True Then
        ie.Visible = True
        txt.MoveStart "character", -1
        txt.findText strCurrentProtocolName
        txt.Select
        txt.ScrollIntoView
    Else
        MsgBox "Protocol " & strCurrentProtocolName & " was not found on the BRD website!", vbInformation + vbOKOnly
    End If
    
    Exit Function
endit:
    Select Case Err.Number
        Case -2147417848 'automation error.  user probably closed ie manually
            Set ie = Nothing
            GoTo beginning
        Case Else
            Debug.Print Err.Number & " " & Err.Description
             MsgBox Err.Number & Err.Description
    End Select
End Function

Open in new window

I haven't tried this but give it a shot to check if IE is open.


   Dim oShApp As Object, oWin As Object, IE As Object
   Dim WScript As Object
    
    Set oShApp = CreateObject("Shell.Application")
    For Each oWin In oShApp.Windows
    If TypeName(oWin.Document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
    Next
    If IE Is Nothing Then _
        Set IE = CreateObject("InternetExplorer.Application")

Open in new window