office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請(qǐng)用微信掃碼)

        

vba完全關(guān)閉IE瀏覽器及調(diào)用IE瀏覽器的簡(jiǎn)單應(yīng)用

2021-04-25 08:00:00
tmtony8
翻譯
20676

在調(diào)用IE對(duì)象時(shí),多次調(diào)用會(huì)出現(xiàn)錯(cuò)誤,防止多次調(diào)用IE對(duì)象,可以把把IE瀏覽器對(duì)象徹底地關(guān)閉

添加一個(gè)IE_Sledgehammer函數(shù)確保IE瀏覽器完全關(guān)閉

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


瀏覽器完全關(guān)閉后,調(diào)用IE瀏覽器

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


下面分享一些關(guān)于調(diào)用IE瀏覽器的常用功能:
1. VBA調(diào)用 InternetExplorer IE瀏覽器組件
Sub IE()
'    Create Internet Explorer Application, going on the internet!
    Set IE = CreateObject("InternetExplorer.Application")
'    Internet Explorer Visible
    IE.Visible = True
'    Internet Explorer Left & Top Position on the screen
    IE.Left = 0
    IE.Top = 0
'    Internet Explorer Height & Width Settings
    IE.Height = 1024
    IE.Width = 1280
'    Internet Explorer Navigate To office-cn
    IE.Navigate "m.mzhfr.cn"
    To stop IE I use a command button with as code under the click event :
'    Stop & Close Internet Explorer
    IE.Stop
    IE.Quit
End Sub



2. 使用VBA打開(kāi)URL地址并在表單中輸入數(shù)據(jù)
'This Must go at the top of your module. It's used to set IE as the active window
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim HWNDSrc As Long
    
 
    'Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
 
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True
 
    'Define URL
    URL = "http://m.mzhfr.cn"
 
    'Navigate to URL
    IE.Navigate URL
 
    ' Statusbar let's user know website is loading
    Application.StatusBar = URL & " is loading. Please wait..."
 
    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop
 
    'Webpage Loaded
    Application.StatusBar = URL & " Loaded"
    
    'Get Window ID for IE so we can set it as activate window
    HWNDSrc = IE.HWND
    'Set IE as Active Window
    SetForegroundWindow HWNDSrc
    
    
    'Find & Fill Out Input Box
    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                             'Activates the Input box (makes the cursor appear)
                Application.SendKeys "{w}", True      'Simulates a 'W' keystroke. True tells VBA to wait
                                                      'until keystroke has finished before proceeding, allowing
                                                      'javascript on page to run and filter the table
                GoTo endmacro
            End If
        End If
    Next
    
    'Unload IE
endmacro:
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
    
End Sub

分享
文章分類
聯(lián)系我們
聯(lián)系人: 王先生
Email: 18449932@qq.com
QQ: 18449932
微博: officecn01
移動(dòng)訪問(wèn)