Lỗi khi bắt Object Internet Explorer bằng lệnh CreateObject("Shell.Application").Windows (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

evolt

Thành viên mới
Tham gia
14/6/13
Bài viết
27
Được thích
4
Mình có 2 đoạn Sub để bắt Object Internet Explorer như sau:
PHP:
Sub CloseIE()
Dim i As Integer
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
With CreateObject("Shell.Application")
    For i = 0 To .Windows.Count - 1
        If .Windows(i).Name = "Internet Explorer" Then Set oBrowser = .Windows(i)
    Next
End With
On Error GoTo 0
If oBrowser Is Nothing Then Exit Sub
oBrowser.Quit
Set oBrowser = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

PHP:
Sub unhideIE()
Dim i As Integer
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
With CreateObject("Shell.Application")
    For i = 0 To .Windows.Count - 1
        If .Windows(i).Name = "Internet Explorer" Then
            If .Windows(i).Visible Then
            .Windows(i).Visible = False
            Else
            .Windows(i).Visible = True
            End If
            Sheet8.CommandButton2.Caption = .Windows(i).Visible
        End If
    Next
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

Không hiểu sao sub CloseIE thì chạy được bình thường còn sub unhideIE thì nó báo lỗi "Invalid procedure call or argument" ở ngay đoạn "If .Windows(i).Name = "Internet Explorer"". Kính nhờ mọi người giải thích giúp mình đoạn này với.

Thêm nữa, tại sao lệnh CreateObject("Shell.Application").Windows("Internet Explorer") lại không chạy được vậy ạ :D
 
Mình có 2 đoạn Sub để bắt Object Internet Explorer như sau:
......
Không hiểu sao sub CloseIE thì chạy được bình thường còn sub unhideIE thì nó báo lỗi "Invalid procedure call or argument" ở ngay đoạn "If .Windows(i).Name = "Internet Explorer"". Kính nhờ mọi người giải thích giúp mình đoạn này với.

Thêm nữa, tại sao lệnh CreateObject("Shell.Application").Windows("Internet Explorer") lại không chạy được vậy ạ :D
Dùng For Each để duyệt Object . Windows([index]) đối số là index nên không thể truyền vào một chuỗi "Internet Explorer"
Không nên dùng ràng buộc lỗi On Error Resume Next
Window Item chỉ có Internet Explorer và File Explorer
Mã:
    Dim WINDW
    For Each WINDW In CreateObject("Shell.Application").Windows
        If WINDW.name = "Internet Explorer" Then
            WINDW.Visible = IIf(Not (WINDW.Visible), True, False)
        End If
    Next
 
Lần chỉnh sửa cuối:
Upvote 0
Window Item chỉ có Internet Explorer và File Explorer
cái này giờ mình mới biết, nhưng sao thỉnh thoảng mình đếm Windows.count nó lại = 3 và trong đó có 1 cái Windows(i).name = "" luôn và nó báo lỗi ở chỗ này.

Mình mới tìm hiểu thêm về windows api, mình dùng lệnh FindWindow("IEFrame", vbNullString) để lấy hWnd của IE, xong rồi có cách nào dùng hWnd đó để gán nó vào thành 1 biến trong VBA không bạn?
 
Upvote 0
cái này giờ mình mới biết, nhưng sao thỉnh thoảng mình đếm Windows.count nó lại = 3 và trong đó có 1 cái Windows(i).name = "" luôn và nó báo lỗi ở chỗ này.

Mình mới tìm hiểu thêm về windows api, mình dùng lệnh FindWindow("IEFrame", vbNullString) để lấy hWnd của IE, xong rồi có cách nào dùng hWnd đó để gán nó vào thành 1 biến trong VBA không bạn?
- Windows.count sẽ đếm có bao nhiêu cửa sổ IE và Explore đang mở. Nếu bạn biết Explore.exe có 2 dạng ,1 là luôn luôn chạy trong hệ thống và bạn không thể tương tác với nó. 2 Khi bạn mở một folder, đó là Explore.exe giao diện người dùng để tương tác.
- Không rõ ý của bạn gán như thế nào. Gán thành Object để điều khiển?
 
Upvote 0
Đúng rồi ý mình là vậy đó :D
Tôi khuyên bạn nên dùng Selenium Webdriver để điều khiển Chrome thay vì IE. Bạn có thể làm mọi điều bạn muốn

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

Sub .......()
    Dim URL As String
    Dim IE As Object
    Dim HWNDSrc As Long
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = "https://....."
    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
    'Set IE as Active Window
    SetForegroundWindow HWNDSrc
    'Unload IE
    Set IE = Nothing
End Sub
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom