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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41367209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:46:25  来源:igfitidea点击:

How can I maximize an IE window created by VBA with SHDocVw.InternetExplorer command?

excelvbaexcel-vbashdocvw

提问by dot.Py

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

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

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?

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

回答by cyboashu

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

对于 Internet 控制,没有固有Window属性。您需要使用 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/

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

回答by UnknownQ

This could be done like this as well.

这也可以这样做。

ie.FullScreen = True

or

或者

ie.TheaterMode = True

Then you don't to declare a function.

那么你不要声明一个函数。