6

As the title says, I'm trying to maximize an internet explorer window that was created using the following command:

Set ie = New SHDocVw.InternetExplorer

Instead of:

Set ie = CreateObject("InternetExplorer.Application")

Here's the full code:

Sub wpieautologin()
Dim ie As SHDocVw.InternetExplorer

Dim NOME_EMPRESA, CNPJ, CPF, COD_ACESSO As String
Dim Lookup_Range As Range

Set ie = New SHDocVw.InternetExplorer
ie.Visible = False
ie.Navigate "http://www8.receita.fazenda.gov.br/simplesnacional/controleacesso/autentica.aspx?id=6"

NOME_EMPRESA = Range("B8").Value
Set Lookup_Range = Range("B12:E500")

CNPJ = Application.WorksheetFunction.VLookup(NOME_EMPRESA, Lookup_Range, 2, False)
CPF = Application.WorksheetFunction.VLookup(NOME_EMPRESA, Lookup_Range, 3, False)
COD_ACESSO = Application.WorksheetFunction.VLookup(NOME_EMPRESA, Lookup_Range, 4, False)

Do
Loop Until ie.readystate = 4
Call ie.Document.GetElementByID("ctl00_ContentPlaceHolder_txtCNPJ").SetAttribute("value", CNPJ)
Call ie.Document.GetElementByID("ctl00_ContentPlaceHolder_txtCPFResponsavel").SetAttribute("value", CPF)
Call ie.Document.GetElementByID("ctl00_ContentPlaceHolder_txtCodigoAcesso").SetAttribute("value", COD_ACESSO)
ie.Visible = True

>'What should I write here to maximize my IE Window? 
>'Already tried a few solutions, but they works only when the IE is created by the command 
>'Set ie = CreateObject("InternetExplorer.Application")

#INSERT COMMAND TO MAXIMIZE WINDOW HERE

End Sub

So, how can I achieve this?

dot.Py
  • 5,007
  • 5
  • 31
  • 52

2 Answers2

9

This could be done like this as well.

ie.FullScreen = True

or

ie.TheaterMode = True

Then you don't to declare a function.

UnknownQ
  • 154
  • 1
  • 3
  • 1
    This is by far better! I think I was here in the past and didn't get past reading the above function and decided it wasn't worth the effort. No to mention the comments Re: `x32/x64` MSOffice issues which this shouldn't have. I like my macro's to be 100% portable. – FreeSoftwareServers Sep 05 '20 at 02:58
  • 1
    Note: I used `SENDKEYS` to interact w/ IE via VBA. Upon testing `FullScreen` acted differently w/ my existing VBA Code, but `TheaterMode` worked as expected. They both look the same. Now that I know this, I would always use `TheaterMode`, but I could have of course adapted to `FullScreen` – FreeSoftwareServers Sep 05 '20 at 20:41
  • 1
    More Notes: After using this macro, when I opened IE for other reasons manually, it was still using `TheaterMode` so I added `TheaterMode = False` to the end before I unloaded IE. – FreeSoftwareServers Sep 05 '20 at 20:47
8

For Internet Control, there's no inherent Window property. You need to use WinAPI.

This code will work :

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

Sub wpieautologin()

Dim ie As SHDocVw.InternetExplorer

Dim NOME_EMPRESA, CNPJ, CPF, COD_ACESSO As String
Dim Lookup_Range As Range

Set ie = New SHDocVw.InternetExplorer
ie.Visible = False

ie.Navigate "http://www8.receita.fazenda.gov.br/simplesnacional/controleacesso/autentica.aspx?id=6"



'// rest of your code....


'/ Win API to maximize it.
'/ Visible prop not required anymore
ShowWindow ie.hwnd, SW_SHOWMAXIMIZED
End Sub

Check other window state at : http://www.techrepublic.com/blog/10-things/10-plus-of-my-favorite-windows-api-functions-to-use-in-office-applications/

cyboashu
  • 10,196
  • 2
  • 27
  • 46
  • 3
    Thanks for your reply! When I use your code, first it says that this code must be updated to a 64bit version... So, instead of `Private Declare Function ShowWindow Lib "user32" _` I must use `Private Declare PtrSafe Function ShowWindow Lib "user32" _`... Ok, first error fixed! So after fixing the error stated above, I get the following error: `Incompatible types` and then the debugger marks the following statement: `ShowWindow ie.hwnd` Do you know why? – dot.Py Dec 28 '16 at 18:25
  • 3
    dont have 64bit excel at my end, but try this link : http://www.jkp-ads.com/articles/apideclarations.asp. – cyboashu Dec 28 '16 at 18:31
  • 3
    @rfw - Change `ByVal hwnd as Long` to `ByVal hwnd as LongPtr`. – Comintern Dec 28 '16 at 18:34
  • 2
    Using the code posted by `cyboashu` and the tweak provided by `Comintern` my code worked perfectly!! Thanks for your time and effort, guys. – dot.Py Dec 28 '16 at 18:39
  • or You could just change `ShowWindow Clng(ie.hwnd)` – Teamothy Nov 02 '20 at 13:52