Nhờ các bạn rút gọn đoạn code. Có thể làm = Do...loop hoặc Do...While...Loop được ko?

Liên hệ QC

phanminhphuong

Thành viên hoạt động
Tham gia
26/7/13
Bài viết
127
Được thích
68
PHP:
Dim oIE As Object ' InternetExplorer.Application
Dim sURL As String
Dim btnInput As Object ' MSHTML.HTMLInputElement
Dim ieDoc As Object  'MSHTML.HTMLDocument
Dim ElementCol As Object
Set oIE = CreateObject("InternetExplorer.Application")
Set ieDoc = oIE.Document
Nhờ các bạn rút gọn đoạn code
Mã:
sURL = "http://diachigigido.com"
If oIE.LocationURL = sURL Then 'Kie^?m tra URL la^`n 1
   Select Case Application.Assistant.DoAlert("NOTICE",  [U74].Value, msoAlertButtonOKCancel, msoAlertIconInfo,  msoAlertDefaultFirst, msoAlertCancelFirst, False) 'Thông báo = tiếng việt UNICODE nội dung ô U74
    Case vbOK
                            
      Set ElementCol = ieDoc.getElementsByTagName("a")
        For Each btnInput In ElementCol
          If btnInput.Title = "Add Subject" Then
            btnInput.Click                                      'An button tao moi
            Exit For
          End If
        Next
        Do While oIE.Busy Or oIE.ReadyState <> 4: DoEvents: Loop
                                
           If oIE.LocationURL = sURL Then 'Nếu địa chỉ trang web ko đổi (Ấn button nhưng ko thực hiện được) - Kie^?m tra URL la^`n 2
             Select Case Application.Assistant.DoAlert("NOTICE", [U74].Value,  msoAlertButtonOKCancel, msoAlertIconInfo, msoAlertDefaultFirst,  msoAlertCancelFirst, False) 'Lại thông báo nội dung ô U74
              Case vbOK
                Set ElementCol = ieDoc.getElementsByTagName("a")
                  For Each btnInput In ElementCol
                    If btnInput.Title = "Add Subject" Then
                      btnInput.Click                                      'An button tao moi
                      Exit For
                    End If
                  Next
                  Do While oIE.Busy Or oIE.ReadyState <> 4: DoEvents: Loop

                    If oIE.LocationURL = sURL Then 'Laji kiểm tra URL lần thứ 3
                       Select Case Application.Assistant.DoAlert("NOTICE", [U74].Value,  msoAlertButtonOKCancel, msoAlertIconInfo, msoAlertDefaultFirst,  msoAlertCancelFirst, False)
                      End Select
                      Application.Visible = True
                      Exit Sub
                    End If
              Case vbCancel
                Application.Visible = True
                Exit Sub
             End Select
           End If
                                
     Case vbCancel
        Application.Visible = True
        Exit Sub
  End Select
End If
 
Lần chỉnh sửa cuối:
PHP:
Dim oIE As Object ' InternetExplorer.Application
Dim sURL As String
Dim btnInput As Object ' MSHTML.HTMLInputElement
Dim ieDoc As Object  'MSHTML.HTMLDocument
Dim ElementCol As Object
Set oIE = CreateObject("InternetExplorer.Application")
Set ieDoc = oIE.Document
Nhờ các bạn rút gọn đoạn code
Mã:
sURL = "http://diachigigido.com"
If oIE.LocationURL = sURL Then 'Kie^?m tra URL la^`n 1
   Select Case Application.Assistant.DoAlert("NOTICE",  [U74].Value, msoAlertButtonOKCancel, msoAlertIconInfo,  msoAlertDefaultFirst, msoAlertCancelFirst, False) 'Thông báo = tiếng việt UNICODE nội dung ô U74
    Case vbOK
                            
      Set ElementCol = ieDoc.getElementsByTagName("a")
        For Each btnInput In ElementCol
          If btnInput.Title = "Add Subject" Then
            btnInput.Click                                      'An button tao moi
            Exit For
          End If
        Next
        Do While oIE.Busy Or oIE.ReadyState <> 4: DoEvents: Loop
                                
           If oIE.LocationURL = sURL Then 'Nếu địa chỉ trang web ko đổi (Ấn button nhưng ko thực hiện được) - Kie^?m tra URL la^`n 2
             Select Case Application.Assistant.DoAlert("NOTICE", [U74].Value,  msoAlertButtonOKCancel, msoAlertIconInfo, msoAlertDefaultFirst,  msoAlertCancelFirst, False) 'Lại thông báo nội dung ô U74
              Case vbOK
                Set ElementCol = ieDoc.getElementsByTagName("a")
                  For Each btnInput In ElementCol
                    If btnInput.Title = "Add Subject" Then
                      btnInput.Click                                      'An button tao moi
                      Exit For
                    End If
                  Next
                  Do While oIE.Busy Or oIE.ReadyState <> 4: DoEvents: Loop

                    If oIE.LocationURL = sURL Then 'Laji kiểm tra URL lần thứ 3
                       Select Case Application.Assistant.DoAlert("NOTICE", [U74].Value,  msoAlertButtonOKCancel, msoAlertIconInfo, msoAlertDefaultFirst,  msoAlertCancelFirst, False)
                      End Select
                      Application.Visible = True
                      Exit Sub
                    End If
              Case vbCancel
                Application.Visible = True
                Exit Sub
             End Select
           End If
                                
           Case vbCancel
           Application.Visible = True
           Exit Sub
     End Select
End If
Theo như mình hiểu thì công việc của bạn là:
1) Ấn nút button có Title = "Add Subject"
2) Nếu trang web ko nhảy sang địa chỉ khác <=> có lỗi, thì nó lại hiện lên thông báo nội dung ô U74 (có thể là kiểm tra lại). Khi hiện thông báo, thì vẫn chỉnh sửa bằng tay được. Sau khi chỉnh sửa thủ công trên web thì chọn nút vbOK trên thông báo lại quay lại thực hiện bước 1, 2
Vậy thì
Mã:
sURL = "http://diachigigido.com"
              
   Do While oIE.LocationURL = sURL
     Select Case Application.Assistant.DoAlert("NOTICE", [U74].Value, msoAlertButtonOKCancel, msoAlertIconInfo, msoAlertDefaultFirst, msoAlertCancelFirst, False)
       Case vbOK
         Set ElementCol = ieDoc.getElementsByTagName("a")
           For Each btnInput In ElementCol
             If btnInput.Title = "Add Subject" Then
               btnInput.Click                                      'An button tao moi
               Exit For
             End If
           Next
           oIE.Visible = True
           Do While oIE.Busy Or oIE.ReadyState <> 4: DoEvents: Loop
       Case vbCancel
           Application.Visible = True
           Exit Sub
      End Select
  Loop
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT
Back
Top Bottom