open and maximise an IE window using vba

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the following code that opens an internet window from Excel using vba, however the IE window never opens up 'maximised' - how can I adapt my code to achieve this?

Code:
Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.navigate "http://www.google.com"
    
    Do While IE.Busy: DoEvents: Loop
    Do While IE.readyState <> 4: DoEvents: Loop
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
ok, so where exactly do the bits of code go, obviously not like I've got them below!!


Code:
Private Declare Function ShowWindow Lib "user32" _
         (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
        Const SW_SHOWMAXIMIZED = 3
    
    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.navigate "http://www.google.com"
    
    Do While IE.Busy: DoEvents: Loop
    Do While IE.readyState <> 4: DoEvents: Loop
    
    ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
 
Upvote 0
well regardless of your code let me explain and you should be able to figure it out...

You need to import a Windows API function first... put this at top... you need to call a function that does not exist in the standard vba references... the function is in a dll that is used to control windows... so you need code to tell vba where the function is, what the name of the function is, and you need to describe the parameters it uses... so the vba compiler will know where to go...

Code:
'/ Win API declaration
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
Const SW_SHOWMAXIMIZED = 3

You are declaring also a constant to use later in the function... the constant is just a number that tells the function how to act... and this function apparently requires you to pass the value 3 in order to tell it to maximize the window

So somewhere in your code you creating an instance of IE...

Code:
Dim IE As Object
Set IE = CreateObject("internetexplorer.application")

so after that in order to set the window state you then do... (has to be after you create IE for obvious reasons)

Code:
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED

or

Code:
ShowWindow IE.hwnd, 3
 
Last edited:
Upvote 0
ok, where does the following code reside?
obviously not in my macro...
Code:
'/ Win API declaration
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
Const SW_SHOWMAXIMIZED = 3
 
Last edited:
Upvote 0
correct, you cant declare a function inside another function/subroutine... so put it anywhere outside the Sub ... End Sub

i should have been more specific when i said "at the top" ... my bad ;)
 
Last edited:
Upvote 0
So is that Insert > Module in the VB Editor?
 
Upvote 0
Last edited:
Upvote 0
Can you be more specific please, have entered the code after my macro and it's not playing (I'm not advanced in vba!)
Code:
Sub Macro250()
'
' Macro250 Macro
'


    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate "http://www.google.com"
    
    ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
    
    Do While IE.Busy: DoEvents: Loop
    Do While IE.ReadyState <> 4: DoEvents: Loop
    
    End Sub
    
    '/ Win API declaration
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
    Const SW_SHOWMAXIMIZED = 3
    
    End Sub
 
Upvote 0
Can you be more specific please, have entered the code after my macro and it's not playing (I'm not advanced in vba!)
Code:
[COLOR=#008000]'IMPORT WINAPI FUNCTIONS HERE[/COLOR]
Sub Macro250()
[COLOR=#008000]' YOU CANT IMPORT IN HERE! :([/COLOR]
' Macro250 Macro
'


    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate "http://www.google.com"
    
    ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
    
    Do While IE.Busy: DoEvents: Loop
    Do While IE.ReadyState <> 4: DoEvents: Loop
End Sub
[COLOR=#008000]'IMPORT WINAPI FUNCTIONS HERE TOO[/COLOR]
'/ Win API declaration
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
Const SW_SHOWMAXIMIZED = 3

look above, i deleted the extra 'End Sub' at the end
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top