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

dot*_*.Py 2 excel vba shdocvw excel-vba

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

Set ie = New SHDocVw.InternetExplorer
Run Code Online (Sandbox Code Playgroud)

代替:

Set ie = CreateObject("InternetExplorer.Application")
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:

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
Run Code Online (Sandbox Code Playgroud)

那么,我该如何实现呢?

小智 8

This could be done like this as well.

ie.FullScreen = True
Run Code Online (Sandbox Code Playgroud)

or

ie.TheaterMode = True
Run Code Online (Sandbox Code Playgroud)

Then you don't to declare a function.


cyb*_*shu 6

对于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
Run Code Online (Sandbox Code Playgroud)

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

  • 感谢您的回复!当我使用你的代码时,首先它说这个代码必须更新到 64 位版本......所以,我必须使用 `Private Declare PtrSafe Function ShowWindow Lib "user32" 而不是 `Private Declare Function ShowWindow Lib "user32" _` _`... 好的,第一个错误已修复!因此,在修复上述错误后,我收到以下错误:`Incompatible types` 然后调试器标记以下语句:`ShowWindow ie.hwnd` 你知道为什么吗? (2认同)
  • 我最后没有 64 位 excel,但试试这个链接:http://www.jkp-ads.com/articles/apideclarations.asp。 (2认同)
  • @rfw-将`ByVal hwnd as Long`更改为`ByVal hwnd as LongPtr`。 (2认同)
  • 使用`cyboashu` 发布的代码和`Comintern` 提供的调整,我的代码完美运行!!谢谢你们的时间和精力,伙计们。 (2认同)