Created
May 7, 2019 18:13
-
-
Save chilismaug/308507c48ec93fff913b495b0bcc48e3 to your computer and use it in GitHub Desktop.
vba with IE from a forum (https://vba-corner.livejournal.com/4623.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' from https://vba-corner.livejournal.com/4623.html | |
'Before you can do anything with the Internet Explorer, of course you'll need one, and you'll need something to address it. The following function achieves exactly this by creating a new instance of the Internet Explorer and returning a pointer to it. | |
'returns new instance of Internet Explorer | |
Function GetNewIE() As SHDocVw.InternetExplorer | |
'create new IE instance | |
Set GetNewIE = New SHDocVw.InternetExplorer | |
'start with a blank page | |
GetNewIE.Navigate2 "about:Blank" | |
End Function | |
'The next function loads a webpage: | |
'loads a web page and returns True or False depending on | |
'whether the page could be loaded or not | |
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, _ | |
i_URL As String) As Boolean | |
With i_IE | |
'open page | |
.Navigate i_URL | |
'wait until IE finished loading the page | |
Do While .ReadyState <> READYSTATE_COMPLETE | |
Application.Wait Now + TimeValue("0:00:01") | |
Loop | |
'check if page could be loaded | |
If .Document.URL = i_URL Then | |
LoadWebPage = True | |
End If | |
End With | |
End Function | |
'But what if the desired page has already been loaded and you just want to use it? | |
'The following function returns a pointer to an already open instance of the Internet Explorer, if it has the desired page loaded. It'll find it by comparing this page's URL with the URLs of open pages in the Internet Explorer. If the page could not be found, the function returns Nothing. | |
'finds an open IE site by checking the URL | |
Function GetOpenIEByURL(ByVal i_URL As String) As SHDocVw.InternetExplorer | |
Dim objShellWindows As New SHDocVw.ShellWindows | |
'ignore errors when accessing the document property | |
On Error Resume Next | |
'loop over all Shell-Windows | |
For Each GetOpenIEByURL In objShellWindows | |
'if the document is of type HTMLDocument, it is an IE window | |
If TypeName(GetOpenIEByURL.Document) = "HTMLDocument" Then | |
'check the URL | |
If GetOpenIEByURL.Document.URL = i_URL Then | |
'leave, we found the right window | |
Exit Function | |
End If | |
End If | |
Next | |
End Function | |
'Sometimes, when you enter an URL to load a page, you'll get redirected, and thus the URL changes. Or your site uses frames and has the same URL for different content, so you can't clearly identify a page by its URL. Then you could check the title of the page instead: | |
'finds an open IE site by checking the title | |
Function GetOpenIEByTitle(i_Title As String, _ | |
Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer | |
Dim objShellWindows As New SHDocVw.ShellWindows | |
If i_ExactMatch = False Then i_Title = "*" & i_Title & "*" | |
'ignore errors when accessing the document property | |
On Error Resume Next | |
'loop over all Shell-Windows | |
For Each GetOpenIEByTitle In objShellWindows | |
'if the document is of type HTMLDocument, it is an IE window | |
If TypeName(GetOpenIEByTitle.Document) = "HTMLDocument" Then | |
'check the title | |
If GetOpenIEByTitle.Document.Title Like i_Title Then | |
'leave, we found the right window | |
Exit Function | |
End If | |
End If | |
Next | |
End Function | |
'(This was tested with IE6. Since IE7 uses tabs for different webpages instead of different windows, this code probably won't work for IE7.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment