How to convert existing vba code from Access to work in Excel?

We have an access DB with a form that displays the target website.  The below code is used to click the refresh button for the website from within Access.  I need to be able to use it in excel for an existing IE window (https://servman/sm/index.do).

Private Sub Command10_Click()
    ' Click the Refresh Button
    
    Dim HTML As HTMLDocument
    Dim btn As HTMLButtonElement
    Dim blnRefreshFound As Boolean
    
    Set HTML = Me.WebBrowser0.Object.Document
    blnRefreshFound = False
    
    For Each btn In HTML.getElementsByTagName("button")
        If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then
            btn.Click
            blnRefreshFound = True
            Exit For
        End If
    Next btn
    
    If Not blnRefreshFound Then
        MsgBox ("No refresh button found")
    End If
End Sub

Open in new window

kbay808Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
I cannot access the site listed but some pointers

You will need to add a reference to "Microsoft Internet Controls"

The WebBrowser object can be found by right clicking in the forms toolbox and selecting Additional Controls > Microsoft We Browser

After this place a break on the line
If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then 

Open in new window

Then and add a watch on the "btn" object so that you can determine how to test for the appropiate button (The watch will enable to look at all available options under the btn object). I cannot help here with out access to the site

I have attached a document which has started this process using Google
Book1.xlsm
0
Robberbaron (robr)Commented:
to do that I believe you need to launch the webpage from within Excel VBA , so that you get the window handle. otherwise no way of knowing which is the desired browser window/tab.

Private Sub Command10_Click()
    ' Click the Refresh Button
    
    Dim HTML As HTMLDocument
    Dim btn As HTMLButtonElement
    Dim blnRefreshFound As Boolean
    
    'Set HTML = Me.WebBrowser0.Object.Document
    Dim myWebPage As String
    myWebPage = "http://www.behindthename.com/random/"
    Set HTML = OpenWebDoc(myWebPage)
    
    blnRefreshFound = False
    
    For Each btn In HTML.getElementsByTagName("button")
        If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then
            btn.Click
            blnRefreshFound = True
            Exit For
        End If
    Next btn
    
    If Not blnRefreshFound Then
        MsgBox ("No refresh button found")
    End If
End Sub

Function OpenWebDoc(targetpage As String) As Object
'Requires reference to "Microsoft Internet Controls"'
    'Dim IE As SHDocVw.InternetExplorer
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    
    'Navigate to the URL'
    IE.Navigate (targetpage)
    
    'No need to show the window'
    IE.Visible = False
    
    'Wait for IE to load the page'
    While IE.Busy: DoEvents: Wend
    Do While IE.ReadyState <> 4: DoEvents: Loop
    
    
    Set OpenWebDoc = IE.Document
    
    
End Function

Open in new window

0
NorieData ProcessorCommented:
Here's code to get a reference to an open IE window.
Sub GetIE_LateBinding()
  
  Dim IE As Object
  
  With CreateObject("Shell.Application").Windows
    
    If .Count > 0 Then
      ' Get IE
      Set IE = .Item(0) ' or .Item(.Count - 1)
    Else
      ' Create IE
      Set IE = CreateObject("InternetExplorer.Application")
      IE.Visible = True
    End If
  
    IE.Navigate "http://suSub GetIE_LateBinding()
  
  Dim IE As Object
  
  With CreateObject("Shell.Application").Windows
    
    If .Count > 0 Then
      ' Get IE
      Set IE = .Item(0) ' or .Item(.Count - 1)
    Else
      ' Create IE
      Set IE = CreateObject("InternetExplorer.Application")
      IE.Visible = True
    End If
  
    ' do your stuff with IE here
    
  End With
End Sub

Open in new window

0
ltlbearand3Commented:
Hi Kbay808,

This looks like it is related to many of the other questions I have helped you with.  We can use a very similar process in this instance.  We may have to adjust for the frames, if this is not in the default frame.  For now, I will assume the default frame has the refresh button.  If not, we may need to run the debug code to find the right frame again.  You can start with this code in Excel, which is very similar to the other code we have used:

' References Required
' MICROSOFT HTML OBJECT LIBRARY
' MICROSOFT INTERNET CONTROLS

Sub ClickRefresh()

    Dim objIE As SHDocVw.InternetExplorer
    Dim objShell As SHDocVw.ShellWindows
    Dim objIEDoc As MSHTML.HTMLDocument
    Dim blnFoundWebSite As Boolean
    Dim blnRefreshFound As Boolean

    ' **** Set the Web Address Here ****
    strURL = "https://servman/sm/index.do"
    
    ' Look for the appropriate open IE Window
    For Each objIE In objShell
        If TypeOf objIE.Document Is MSHTML.HTMLDocument Then
            Set objIEDoc = objIE.Document
            If Not (objIEDoc Is Nothing) Then
                If objIEDoc.URL = strURL Then
                    blnFoundWebSite = True
                    Exit For
                End If
            End If
        End If
    Next
    
    
    If blnFoundWebSite Then
        blnRefreshFound = False
        
        For Each btn In objIEDoc.getElementsByTagName("button")
            If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then
                btn.Click
                blnRefreshFound = True
                Exit For
            End If
        Next btn
    
        If Not blnRefreshFound Then
            MsgBox ("No refresh button found")
        End If
    Else
        MsgBox strURL & " was not found" & vbCrLf & "in an open Internet Explorer Window", vbExclamation, "No Page Found"
    End If
    
    Set objIE = Nothing
    Set objShell = Nothing
    Set objIEDoc = Nothing
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbay808Author Commented:
It worked perfect.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.