Creating InternetExplorer object in VBA – controlling window state

Posted on 2004-08-16
Last Modified: 2007-12-19

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
Question by:IainInLondon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 11814543
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!

Expert Comment

ID: 11814983
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 :)

Author Comment

ID: 11818731
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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 11819742
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

Accepted Solution

fattumsdad earned 100 total points
ID: 11821149
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

Assisted Solution

jmacmicking earned 25 total points
ID: 11823019
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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question