所以我有这个代码,我正在为我的部门工作,去一个网站输入数据,点击运行并下载csv文件到工作表。它在我的个人电脑和其他部门使用的电脑上运行得很好。我们都使用相同版本的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
当此代码由其他部门的成员运行时,它只是打开网站,不输入任何内容,也不按网站上的RUN按钮。
你知道是什么原因导致的吗?什么设置或者别的什么。我验证了两个PC的VBA参考都有,没有"位置缺失路径"。
您从未检查过网页是否已完成加载!
您的On Error Resume Next
语句也隐藏了任何错误,并阻止您通过修复代码采取适当的补救措施。请报告具体的错误编号和引起错误的行。我有一个想法,哪一行和哪个错误,我的答案是:
Type 91 error on line: Set HTMLDOC = .Document
为什么?
当你等待时,Application.Wait
是等待浏览器加载的一种非常无效的方法,2秒可能并不总是足够的时间。我能想到的最好的处理方法,假设你得到一个type 91错误,像这样:
如何为浏览器控件创建一个等待就绪的循环
基本上,你需要将线程放入循环中,直到浏览器加载页面。在伪代码中,你做的是:
Do Until Browser Is Loaded
DoEvents 'etc.
Loop
使用WinAPI睡眠函数
您需要使用WinAPI Sleep
函数(很多人使用DoEvents
或Application.Wait
,但我在这里发现Sleep
是可取的)。
因为它是一个Declare
,你必须把它放在一个普通的模块中,它不能放在一个类或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
是的,总会有例外。我很少做基于web的自动化,我所做的大部分事情只是在这里修修补补,帮助人们解决问题。我注意到的是,越来越多的网站是动态服务(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
你可能想要添加一个迭代器/计数器,就像我在那里做的那样,并模拟一个超时,否则这可能会进入一个无限循环
感谢您提供的所有很棒的技巧。这背后真正的错误是一个Windows设置。UAC:
http://windows.microsoft.com/en-us/windows7/products/features/user-account-control把它放在最下面的"never notify"可以立即解决这个问题。