如何使用SHDocVw.InternetExplorer命令最大化由VBA创建的IE窗口?

时间:2016-12-28 17:50:00

标签: excel vba excel-vba shdocvw

正如标题所说,我正在尝试最大化使用以下命令创建的Internet Explorer窗口:

Set ie = New SHDocVw.InternetExplorer

而不是:

Set ie = CreateObject("InternetExplorer.Application")

这是完整的代码:

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

那么,我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:7)

这也可以这样做。

ie.FullScreen = True

ie.TheaterMode = True

那么您就无需声明函数。

答案 1 :(得分:5)

对于Internet控制,没有固有的Window属性。您需要使用WinAPI。

此代码可以使用:

'/ 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

检查其他窗口状态:http://www.techrepublic.com/blog/10-things/10-plus-of-my-favorite-windows-api-functions-to-use-in-office-applications/