I'm trying to interact with the scroll bar on an IE page using VB.Net in Visual Studio 2017. The Webpage is already open and I find it using SHDocVw. The only way I have found to interact with the scroll bar is using the HTMLWindow object but I'm having trouble converting the InternetExplorer object to HTMLWindow, and currently getting an error:
System.InvalidCastException HResult=0x80004002 Message=Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to class type 'System.Windows.Forms.HtmlDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
What I'm trying to do is move down the page programmatically taking screen shots along the way. I'm going to use SnagIt to capture the screen shot (if you are wondering why I'm not using the scroll screen shot feature in SangIt, it crashes every time I add that line to the API). I need to know when I've reached the end of the page so I don't keep take screen shots endlessly. Can someone please help me with how to do this, or hoe to fix SangIt?
Here is what I have so far:
Sub newScreenShot()
activeIEWindow("http://inside.michigan.gov/dtmb/Pages/default.aspx")
Dim WebBrowser1 As HtmlWindow
Dim temp As HtmlDocument
temp = ie_window.Document
WebBrowser1 = temp.Window
WebBrowser1.ScrollTo(0, 12)
End Sub
Sub activeIEWindow(url As String)
Dim PV As New PortalVerification
'ie_window = PV.findIEWindow(url, "url", "False")
ie_window = PV.findIEWindow(url, "url")
GlobalVariables.SetForegroundWindow(ie_window.HWND)
GlobalVariables.Sleep(1000) 'sleep 1 second to make sure that the window is the active one
End Sub
Function findIEWindow(identifer As String, idtype As String, Optional AddressBar As String = "") As InternetExplorer
findIEWindow = Nothing
For Each window As InternetExplorer In New ShellWindows()
If TypeOf window.Document Is mshtml.HTMLDocument Then
If LCase(idtype) = "name" Then
If window.LocationName Like identifer Then
findIEWindow = window
Exit For
End If
ElseIf LCase(idtype) = "url" Then
If AddressBar <> "" Then
If window.AddressBar = AddressBar Then
If window.LocationURL Like identifer Then
'WS.Range("A1").Value = win.LocationURL
findIEWindow = window
Exit For
End If
End If
Else
If window.LocationURL Like identifer Then
findIEWindow = window
Exit For
End If
End If
End If
End If
Next
End Function
User contributions licensed under CC BY-SA 3.0