Solved

Creating InternetExplorer object in VBA – controlling window state

Posted on 2004-08-16
7
3,328 Views
Last Modified: 2007-12-19
Hello

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
Iain
0
Comment
Question by:IainInLondon
  • 3
  • 2
7 Comments
 
LVL 1

Expert Comment

by:fattumsdad
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!
0
 
LVL 1

Expert Comment

by:fattumsdad
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 :)
0
 
LVL 1

Author Comment

by:IainInLondon
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.

Thanks
Iain
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:jmacmicking
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 http://www.mentalis.org/index2.shtml).  

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
     Else
        ' Turn off the TopMost attribute.
        SetWindowPos hWnd, conHwndNoTopmost, 0, 0, 0,_ 0, conSwpNoActivate Or conSwpShowWindow
     End If
  End Sub
   
0
 
LVL 1

Accepted Solution

by:
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)
   Else
      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)
   Loop
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
0
 
LVL 5

Assisted Solution

by:jmacmicking
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_SHOWNORMAL = 1
Const SW_NORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_MAXIMIZE = 3
Const SW_SHOWNOACTIVATE = 4
Const SW_SHOW = 5
Const SW_MINIMIZE = 6
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNA = 8
Const SW_RESTORE = 9
Const SW_SHOWDEFAULT = 10
Const SW_MAX = 10
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now