I post this response with a similar attitude ... I'm willing to put in the hours to get cool stuff done - but I'm probably not doing it the "right" way ... and I'm borrowing from a ton of other posts around the net...
Here's how I'm doing it now ... after the code I'll explain a few things I plan to do, and the known problems...
** Requires reference to Microsoft Internet Explorer (browse for SHDocVw.dll if you can't see it)
Code:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function ShowWindow& Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long)
'Declare constants (for API functions)
Public Const SW_SHOWNORMAL As Long = 1
Private Sub cmdOpenLink_Click()
Dim strSiteAddress As String
Dim myIE As SHDocVw.InternetExplorer
Dim Newsite As Object
strFDRAddress = "http://www.google.com"
Set myIE = GetOpenIEByURL(strSiteAddress)
If Not myIE Is Nothing Then
ShowWindow& myIE.hWnd, SW_SHOWNORMAL
SetForegroundWindow myIE.hWnd
myIE.Visible = True
Else
Set Newsite = CreateObject("InternetExplorer.application")
Newsite.Navigate strSiteAddress
Newsite.FullScreen = True
Newsite.FullScreen = False
Newsite.Visible = True
ShowWindow Newsite.hWnd, SW_SHOWNORMAL
SetForegroundWindow Newsite.hWnd
End If
End Sub
'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 InStr(GetOpenIEByURL.Document.URL, i_URL) > 0 Then
'leave, we found the right window
Exit Function
End If
End If
Next
End Function
The known problem (would love an answer!)
* With IE 7 and beyond, there might be tabs within a window ... brief experiment shows that if you open google then activate another site on another tab ... this code will activate the IE WINDOW - but not the correct tab.
Notes:
1. The FullScreen on then off is an attempt to resolve this. On my machine, this hides the tabs ... so I'm hoping my user will open the site with my code then go back to it repeatedly and never be able to open another tab.
2. I prefer "early binding" because you know at compile if you have problems. However, experience says that some percentage of users won't touch this feature (I'm doing a right-click on an element of a list to launch a website related... not primary feature of my application). And of the people who use my tool, there is definitely SOMEONE who won't have the right reference. So - this code is half done ... I use early binding (set the reference and DIM the object the RIGHT way) so that I can see all the properties and methods as I type the code. But then I'll convert to late binding (have no reference and DIM as a generic Object ... as the "NewSite" is done). My plan is to make it all late binding and put it in my application.
3. SetForeGroundWindow works great. The ShowWindow is just there in case the window is minimized. The two together work fantastic.
4. the function was copied from:
http://vba-corner.livejournal.com/4623.html and I just edited the search to be Instr instead of = because my webapp uses sub-pages. your mileage may vary.
-Jon B