Creating InternetExplorer object in VBA – controlling window state


I’m using the following piece of code in Excel VBA to open and control and internet explorer session.

Dim IESession As SHDocVw.InternetExplorer
Set IESession = New SHDocVw.InternetExplorer

With IESession
    .Visible = True
    .Navigate MyURL
End With

This works well.

Is it possible to control how the window is opened – Maximized, Minimized or Restored?

One of the properties of the Internet Explorer object is the HWND.  (Not sure if my terminology is quite accurate!)  Could the HWND be used somehow to control the window state?

Thank you
Who is Participating?
fattumsdadConnect With a Mentor Commented:
I just tested this one...  it will minimize IE upon opening.  I used a command button to launch IE from Excel.  You can edit the code to your needs:

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long

Private Const GW_HWNDNEXT = 2
Private Const SW_SHOWMINIMIZED = 2

Public Sub MinimizeWindow(TitleContains As String)
   Dim hWndApp As Long
   Dim nRet As Long
   hWndApp = FindWindowPartial(TitleContains)
   If hWndApp Then
      nRet = ShowWindow(hWndApp, SW_SHOWMINIMIZED)
      MsgBox "No matching applications found."
   End If
End Sub

Public Function FindWindowPartial(TitleStart As String) As Long
   Dim hWndTmp As Long
   Dim nRet As Long
   Dim TitleTmp As String

   hWndTmp = FindWindow(vbNullString, vbNullString)
   Do Until hWndTmp = 0
      If GetParent(hWndTmp) = 0 Then
         TitleTmp = Space(256)
         nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
         If nRet Then
            TitleTmp = UCase(Left(TitleTmp, nRet))
            If InStr(TitleTmp, UCase(TitleStart)) Then
                FindWindowPartial = hWndTmp
                Exit Do
            End If
        End If
      End If
      hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
End Function

Sub Button1_Click()

    Dim IESession As SHDocVw.InternetExplorer
    Set IESession = New SHDocVw.InternetExplorer

    With IESession
        .Visible = True
        .Navigate MyURL
    End With
    MinimizeWindow Trim(IESession)

End Sub
You can try specifying the height / width in the code.  For example:

Dim IESession As SHDocVw.InternetExplorer
Set IESession = New SHDocVw.InternetExplorer

With IESession
    .IESession.Height = "500"
    .IESession.Width = "500"
    .Visible = True
    .Navigate MyURL
End With

Hope that helps!
Disregard my last post, I overlooked your "With" statement.   The code should look like this:

Dim IESession As SHDocVw.InternetExplorer
Set IESession = New SHDocVw.InternetExplorer

With IESession
    .Height = "500"
    .Width = "500"
    .Visible = True
    .Navigate MyURL
End With

As for HWND, I believe that is a read only property that is used to return a value.  I could be wrong there, though.  For laughs, try adding something like:

MsgBox IESession.HWND

And see what you get :)
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

IainInLondonAuthor Commented:
fattumsdad hello.

The height and width do have a contolling effect, which is good.

If I do MsgBox IESession.HWND it does return a number which I thought could be used by some other routine to control the window state.

Also, If you restore an internet window to the size you want it to be, which for my needs would be very small, when you close it, it's saved.  So when you run the above code, the IE window is the same size as you saved it, this too is good.

However.  It's at the back.  So any message boxes don't appear in the Excel session, the Excel program just flashes in the bottom task bar(I'm using XP).  So I guess I would like to bring the Excel Session to the front, or send the Internet to the back.

This is a fairly minor issue.  I won't leave the question outstanding for too long.

Knowing how to contol the window state with the HWND would be useful though, I think.

You can use the HWND in several API calls to effect window size and position.  In particular, SetWindowPlacement, SetWindowPos and SendMessage are all useful.  SetWindowPos, for instance, allows you to set the Z-Order of the window (to move it to the front).

 For a good how-to on using API functions in general and these messages in particular take a look at AllAPI.Net (url is  

Following is MS's example code for using SetWindowPos:

  Declare Function SetWindowPos Lib "user32" Alias_ "SetWindowPos" (ByVal hwnd As Long, ByVal_ hWndInsertAfter As Long, ByVal x As Long, ByVal y As_ Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags_ As Long) As Long

  ' Set some constant values (from WIN32API.TXT).
  Const conHwndTopmost = -1
  Const conHwndNoTopmost = -2
  Const conSwpNoActivate = &H10
  Const conSwpShowWindow = &H40

  Private Sub mnuTopmost_Click ()
     ' Add or remove the check mark from the menu.
     mnuTopmost.Checked = Not mnuTopmost.Checked
     If mnuTopmost.Checked Then
        ' Turn on the TopMost attribute.
        SetWindowPos hWnd, conHwndTopmost, 0, 0, 0, 0,_ conSwpNoActivate Or conSwpShowWindow
        ' Turn off the TopMost attribute.
        SetWindowPos hWnd, conHwndNoTopmost, 0, 0, 0,_ 0, conSwpNoActivate Or conSwpShowWindow
     End If
  End Sub
jmacmickingConnect With a Mentor Commented:
One note on the last post; he's got the HWND (it's part of the application properties) so the ShowWindow lines (the first, where it's declared, the Const declaration for minimize and the actual function call itself) are the only really relevant lines.  In addition to minimizing, the following constants can be used for the other window states he wanted:

Const SW_SHOW = 5 'Show the window in it's current state
Const SW_SHOWMAXIMIZED = 3 'Show the window maximized
Const SW_SHOWNORMAL = 1 'Show the window in it's "normal" (resizable, usually) state

All these constants make the object window the active window.  There are also variations for setting the window state without changing the active window.  The full list follows this post.

Const SW_HIDE = 0
Const SW_NORMAL = 1
Const SW_SHOW = 5
Const SW_SHOWNA = 8
Const SW_RESTORE = 9
Const SW_MAX = 10
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.