【问题标题】:VBA HTML not running on all computersVBA HTML 未在所有计算机上运行
【发布时间】:2014-07-24 01:13:44
【问题描述】:

因此,我正在为我的部门编写此代码,该代码将转到网站输入数据,单击运行并将 csv 文件下载到工作表。它在我的 PC 和其他部门使用的计算机上的个人资料上运行良好。我们都使用相同版本的 windows、excel 和 IE。当我让其他部门的人运行宏时,它会打开网站,但从不将数据输入到字段中,尽管该网站的编码与我登录时完全相同。

Sub testmetric()



Dim appIE As Object
Dim Obj As Object

On Error Resume Next

Sheets("Audit").Select

Selection.AutoFilter

Sheets("Audit").Cells.Clear



Application.ScreenUpdating = False

Application.ScreenUpdating = True
Set appIE = CreateObject("InternetExplorer.Application")


sURL = "http://cctools/rportal/main.php?p=agentavaya"

' Instructes the macro to open IE and navigate to sURL.
With appIE
    .Navigate sURL
    .Visible = True

    Application.Wait Now + TimeValue("00:00:02")
     Set HTMLDOC = .Document
End With



Application.Wait Now + TimeValue("00:00:02")


appIE.Document.getElementById("agentLob").selectedIndex = "3"

appIE.Document.getElementById("timezone").selectedIndex = "1"




appIE.Document.getElementById("sdate").Value = Range("Date_Start")


   appIE.Document.getElementById("edate").Value = Range("Date_End")





   appIE.Document.getElementById("stenure").Value = Range("TenStart")

   appIE.Document.getElementById("etenure").Value = Range("TenEnd")





Application.Wait Now + TimeValue("00:00:02")

For Each Btninput In appIE.Document.getElementsByTagName("INPUT")
    If Btninput.Value = " Run " Then
        Btninput.Click
        Exit For
    End If
     Next



     Application.Wait Now + TimeValue("00:00:02")

Call CSV


appIE.Quit

Sheets("Audit").Select
Range("A1").Select
Sheets("audit").Paste



  End Sub

Sub CSV()
sCSVLink = "http://cctools/rportal/csvexport.php"
sfile = "csvexport.php"
ssheet = "Sheet10"

Set wnd = ActiveWindow

Application.ScreenUpdating = False


Workbooks.Open Filename:=sCSVLink

Windows(sfile).Activate
ActiveSheet.Cells.Copy
wnd.Activate
Range("A1").Select
Sheets("Audit").Paste
Application.DisplayAlerts = False
Windows(sfile).Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Sheets("Audit").Select
Range("A1").Select
Sheets("audit").Paste


End Sub

当此代码由其他部门的成员运行时,它只会打开网站,不输入任何内容,也不会按网站上的“运行”按钮。

关于可能导致此问题的任何想法?什么设置什么的。我验证了两个 PC 的 VBA 引用都在那里,并且没有 "Locations are missing paths" 。

【问题讨论】:

  • 第一步是注释掉On Error Resume Next
  • 谢谢蒂姆。我将返回修复该问题的结果。有点打我的头,因为应该想到这一点来显示它存在什么错误。非常感谢你。如果我无法修复它,希望您关注以防遇到更多错误。
  • 您笼统地假设 4 秒始终足以加载文档,我保证并非总是如此。如果您尝试在文档准备好之前访问该文档,您的代码将会失败。使用等待循环:stackoverflow.com/questions/19334880/…
  • 可能是安全设置不同。在两个浏览器中打开 Internet 选项 -> 安全 -> 自定义级别并查找差异。

标签: html excel vba internet-explorer


【解决方案1】:

您永远不会检查网页是否已完成加载!

您的On Error Resume Next 语句还隐藏了任何错误,并阻止您通过修复代码采取适当的补救措施。请报告具体的错误号和引发错误的行。我知道哪条线和哪条错误,我的答案就是为此:

在线输入91错误:Set HTMLDOC = .Document

为什么?

当您等待时,Application.Wait 是一种非常无效的等待浏览器加载的方法,而且 2 秒可能并不总是足够的时间。假设您遇到 91 型错误,我能想到的最好的处理方法是这样的:

如何为浏览器控件创建一个就绪等待循环

基本上,您需要将线程置于循环中,直到浏览器加载页面。在伪代码中,你正在做:

Do Until Browser Is Loaded
   DoEvents 'etc.
Loop

使用 WinAPI 睡眠功能

您将需要使用 WinAPI Sleep 函数(很多人使用 DoEventsApplication.Wait 但我在这里发现 Sleep 更可取。

因为是Declare,所以必须放在普通模块中,不能放在class或userform模块中。

'## This needs to go in an ordinary code module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

调用 WinAPI 休眠函数

Sleep 250   'sleep for 1/4 of a second, or Sleep 1000 to sleep for a full second, etc.

把它们放在一起

您需要在.Navigate 方法之后立即放置一个调用此函数的Do ... Loop

   '## This goes in where you do Navigate method:
    With appIE
        .Navigate sURL

        Do
            Sleep 250
        Loop While Not .ReadyState <> 4 'READYSTATE_COMPLETE

        Set HTMLDoc = .Document
    End With

如果还是不行……

是的,总会有例外。我很少做基于网络的自动化,我所做的大部分工作只是修补这里的东西来帮助人们。我注意到越来越多的网站是动态服务的(也许是 AJAX?),在这些情况下,浏览器可能是 .ReadyState = READYSTATE_COMPLETE 甚至是 .Busy = False,但 .Document 仍然是 Nothing

如果发生这种情况,您可以放置​​一个辅助循环,例如:

Dim numberOfAttempts
Do While .Document Is Nothing
    Sleep 250
    numberOfAttempts = numberOfAttempts + 1
    If numberOfAttempts > 100 Then 
        MsgBox "This is taking too long, try again later."
        Exit Do
    End If
Loop

您可能希望像我在那里所做的那样添加一个迭代器/计数器并模拟超时,否则这可能会进入无限循环。

【讨论】:

    【解决方案2】:

    感谢您提供的所有重要提示,以实施其中的大量提示。这背后的真正错误是 Windows 设置。 UAC:

    http://windows.microsoft.com/en-us/windows7/products/features/user-account-control 将其设置到底部“从不通知”立即修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-17
      • 2022-01-09
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2017-04-03
      相关资源
      最近更新 更多