nmdong.dbi
Thành viên mới
- Tham gia
- 8/9/20
- Bài viết
- 12
- Được thích
- 0
Trong quá trình chạy code kết xuất tự động một loại báo cáo từ website nội bộ em gặp báo lỗi như sau. Máy tính của em đang sử dụng thì không có lỗi này. Chuyển qua máy khác lại bị báo lỗi như hình. Mong các cao thủ chỉ giáo giúp nguyên nhân ạ. Em trân trọng cảm ơn...
Sub Automate_IE_Export_Data64()
Dim sht As Worksheet
Set sht = Worksheets("TT")
Dim LastRow As Long
Dim i As Long
LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
Dim IE As Object
Set objShell = CreateObject("WScript.Shell")
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("XXX")
Do While IE.readyState <> 4 Or IE.Busy: DoEvents: Loop
For i = 2 To LastRow
For Each el In IE.document.getElementsByTagName("textarea")
If el.Name = "MaDTNT" Then
el.Value = sht.Range("B" & i).Value
Exit For
End If
Next
For Each el In IE.document.getElementsByTagName("select")
If el.Name = "tuQuy" Then
el.Value = sht.Range("D" & i).Value
End If
If el.Name = "denQuy" Then
el.Value = sht.Range("F" & i).Value
End If
Next
For Each el In IE.document.getElementsByTagName("input")
If el.Name = "tuNam" Then
el.Value = sht.Range("E" & i).Value
End If
If el.Name = "denNam" Then
el.Value = sht.Range("G" & i).Value
Exit For
End If
Next
For Each el In IE.document.getElementsByTagName("button")
If el.Name = "KetXuat" Then
el.Click
Exit For
End If
Next
Application.Wait DateAdd("s", 2, Now)
'Application.SendKeys "%{S}"
'Application.SendKeys "{ENTER}"
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Set o = New CUIAutomation
Dim h As Long
h = IE.Hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
Application.Wait DateAdd("s", 3, Now)
Next i
End With
endmacro:
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Sub Automate_IE_Export_Data64()
Dim sht As Worksheet
Set sht = Worksheets("TT")
Dim LastRow As Long
Dim i As Long
LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
Dim IE As Object
Set objShell = CreateObject("WScript.Shell")
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("XXX")
Do While IE.readyState <> 4 Or IE.Busy: DoEvents: Loop
For i = 2 To LastRow
For Each el In IE.document.getElementsByTagName("textarea")
If el.Name = "MaDTNT" Then
el.Value = sht.Range("B" & i).Value
Exit For
End If
Next
For Each el In IE.document.getElementsByTagName("select")
If el.Name = "tuQuy" Then
el.Value = sht.Range("D" & i).Value
End If
If el.Name = "denQuy" Then
el.Value = sht.Range("F" & i).Value
End If
Next
For Each el In IE.document.getElementsByTagName("input")
If el.Name = "tuNam" Then
el.Value = sht.Range("E" & i).Value
End If
If el.Name = "denNam" Then
el.Value = sht.Range("G" & i).Value
Exit For
End If
Next
For Each el In IE.document.getElementsByTagName("button")
If el.Name = "KetXuat" Then
el.Click
Exit For
End If
Next
Application.Wait DateAdd("s", 2, Now)
'Application.SendKeys "%{S}"
'Application.SendKeys "{ENTER}"
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Set o = New CUIAutomation
Dim h As Long
h = IE.Hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
Application.Wait DateAdd("s", 3, Now)
Next i
End With
endmacro:
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
File đính kèm
Lần chỉnh sửa cuối: