分享

vba完全关闭IE浏览器及调用IE浏览器的简单应用

 hqpek 2023-04-25 发布于北京

图片

在调用IE对象时,多次调用会出现错误,防止多次调用IE对象,可以把把IE浏览器对象彻底地关闭

添加一个IE_Sledgehammer函数确保IE浏览器完全关闭

Sub IE_Sledgehammer()
Dim objWMI As Object, objProcess As Object, objProcesses As Object
Set objWMI = GetObject("winmgmts://.")
Set objProcesses = objWMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'")
For Each objProcess In objProcesses
On Error Resume Next
Call objProcess.Terminate
Next
Set objProcesses = Nothing: Set objWMI = Nothing
End Sub

图片

浏览器完全关闭后,调用IE浏览器

Call IE_Sledgehammer
Set ie = CreateObject("InternetExplorer.Application")

下面分享一些关于调用IE浏览器的常用功能:
1. VBA调用 InternetExplorer IE浏览器组件

Sub IE()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

IE.Left = 0
IE.Top = 0
IE.Height = 1024
IE.Width = 1280
' Internet Explorer Navigate To office-cn
IE.Navigate "www."
To stop IE I use a command button with as code under the click event :
IE.Stop
IE.Quit
End Sub

2. 使用VBA打开URL地址并在表单中输入数据


Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

Sub Automate_IE_Enter_Data()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long



Set IE = CreateObject("InternetExplorer.Application")


IE.Visible = True


URL = "https://www."


IE.Navigate URL


Application.StatusBar = URL & " is loading. Please wait..."

Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop

Application.StatusBar = URL & " Loaded"

HWNDSrc = IE.HWND
SetForegroundWindow HWNDSrc

n = 0

For Each itm In IE.document.all
If itm = "[object HTMLInputElement]" Then
n = n + 1
If n = 3 Then
itm.Value = "orksheet"
itm.Focus
Application.SendKeys "{w}", True
GoTo endmacro
End If
End If
Next


endmacro:
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

End Sub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多