Vba 飞行员货运跟踪宏在一台计算机上工作,而不是在另一台计算机上工作
最近我们的办公室升级到了新的笔记本电脑。幸运的是,从Windows 7切换到Windows 10,从台式机切换到笔记本电脑,除了需要在Pilotdelivers.com上启用弹出窗口外,我们在运行此宏时没有遇到任何问题。几周来,我们在两台不同的笔记本电脑上运行了这个宏,一切都很顺利 今天,在一台笔记本电脑上,宏不再正常工作,但在另一台电脑上,它却很好。我检查以确保弹出窗口已启用,并且两台计算机都从网络上的同一个excel工作表运行。我重新启动了计算机两次,并在没有其他应用程序打开的情况下运行了宏。笔记本电脑是同一型号,同时安装。相同的软件安装和更新。有问题的笔记本电脑是由我不太懂电脑的同事使用的,而正在工作的笔记本电脑是我的。所以有可能他改变了一些他不应该改变的设置,但我不确定要检查什么 它似乎跳过了单击链接以打开新选项卡的代码部分 宏应该做什么:Vba 飞行员货运跟踪宏在一台计算机上工作,而不是在另一台计算机上工作,vba,excel,Vba,Excel,最近我们的办公室升级到了新的笔记本电脑。幸运的是,从Windows 7切换到Windows 10,从台式机切换到笔记本电脑,除了需要在Pilotdelivers.com上启用弹出窗口外,我们在运行此宏时没有遇到任何问题。几周来,我们在两台不同的笔记本电脑上运行了这个宏,一切都很顺利 今天,在一台笔记本电脑上,宏不再正常工作,但在另一台电脑上,它却很好。我检查以确保弹出窗口已启用,并且两台计算机都从网络上的同一个excel工作表运行。我重新启动了计算机两次,并在没有其他应用程序打开的情况下运行了宏
Dim ieDOC As HTMLDocument
Set ieDOC = ie.document
Set htmlColl = ieDOC.getElementsByTagName("a")
For Each htmlInput In htmlColl
If htmlInput.ID = "clickElement" Then
htmlInput.Click
Exit For
End If
Next htmlInput
ie.Quit
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
Set ie2 = shellWins.Item(1)
End If
完整代码如下:
Sub PilotTracking()
Dim ProURL As String
Dim ie As Object
Dim ie2 As Object
Dim RowCount As Integer
Dim i As Integer
Dim html_Document As HTMLDocument
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim htmlInput As MSHTML.HTMLInputElement
Dim shellWins As ShellWindows
Dim htmlColl2 As MSHTML.IHTMLElementCollection
Dim htmlInput2 As MSHTML.HTMLInputElement
Dim marker As Integer
RowCount = 0
ProURL = "http://www.pilotdelivers.com/"
Do While Not ActiveCell.Offset(RowCount, -5).Value = ""
Set ie = CreateObject("InternetExplorer.application")
With ie
.Visible = True
.navigate ProURL
Do Until Not ie.Busy And ie.readyState = 4: DoEvents: Loop
End With
Set Doc = ie.document 'works don't delete
Doc.getElementById("tbShipNum").innerHTML = ActiveCell.Offset(RowCount, -5).Value 'works don't delete
Doc.getElementById("btnTrack").Click 'works don't delete
Do Until Not ie.Busy And ie.readyState = 4: DoEvents: Loop
i = 0
Do While i < 4
WaitHalfSec
i = i + 1
Loop
Do Until Not ie.Busy And ie.readyState = 4: DoEvents: Loop
Dim ieDOC As HTMLDocument
Set ieDOC = ie.document
Set htmlColl = ieDOC.getElementsByTagName("a")
For Each htmlInput In htmlColl
If htmlInput.ID = "clickElement" Then
htmlInput.Click
Exit For
End If
Next htmlInput
ie.Quit
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
Set ie2 = shellWins.Item(1)
End If
i = 0
Do While i < 8
WaitHalfSec
i = i + 1
Loop
Do Until Not ie2.Busy And ie2.readyState = 4: DoEvents: Loop
Set htmlColl2 = ie2.document.getElementsByTagName("td")
For Each htmlInput2 In htmlColl2
If htmlInput2.className = "dxgv" Then
If ActiveCell.Offset(RowCount).Value = "" Then
ActiveCell.Offset(RowCount).Value = htmlInput2.innerText
Else
If ActiveCell.Offset(RowCount).Value <> "DELIVERED" Then
ActiveCell.Offset(RowCount, -2).Value = ""
Else
ActiveCell.Offset(RowCount, -2).Value = htmlInput2.innerText
End If
Exit For
End If
End If
Next htmlInput2
ie2.Quit
Set shellWins = Nothing
Set ie = Nothing
Set ie2 = Nothing
RowCount = RowCount + 1
Loop
Set shellWins = Nothing
Set ie = Nothing
Set ie2 = Nothing
End Sub
Sub WaitHalfSec()
Dim t As Single
t = Timer + 1 / 2
Do Until t < Timer: DoEvents: Loop
End Sub
Sub-PilotTracking()
作为字符串的Dim ProURL
模糊的物体
作为对象的尺寸ie2
将行计数设置为整数
作为整数的Dim i
作为HTMLDocument的Dim html_文档
将htmlColl变暗为MSHTML.IHTMLElementCollection
将htmlInput设置为MSHTML.HTMLInputElement
暗淡的外壳作为外壳窗口
将htmlColl2设置为MSHTML.IHTMLElementCollection
将htmlInput2设置为MSHTML.HTMLInputElement
将标记变暗为整数
行计数=0
ProURL=”http://www.pilotdelivers.com/"
不激活单元格时执行此操作。偏移量(行计数,-5)。Value=“”
设置ie=CreateObject(“InternetExplorer.application”)
与ie
.Visible=True
.浏览ProURL
直到ie.Busy和ie.readyState=4:DoEvents:Loop
以
Set Doc=ie.document“作品不删除”
Doc.getElementById(“tbShipNum”).innerHTML=ActiveCell.Offset(RowCount,-5)。值“works不删除”
Doc.getElementById(“btnTrack”)。单击“工作不删除”
直到ie.Busy和ie.readyState=4:DoEvents:Loop
i=0
当我<4岁的时候做
等待半秒
i=i+1
环
直到ie.Busy和ie.readyState=4:DoEvents:Loop
Dim ieDOC作为HTMLDocument
设置ieDOC=ie.document
设置htmlColl=ieDOC.getElementsByTagName(“a”)
对于每个HTMLIN,输入htmlColl
如果htmlInput.ID=“clickElement”,则
htmlInput。单击
退出
如果结束
下一个htmlInput
即退出
设置shellWins=新的ShellWindows
如果shellWins.Count>0,则
设置ie2=shellWins.Item(1)
如果结束
i=0
当我<8岁的时候做
等待半秒
i=i+1
环
直到没有ie2.Busy和ie2.readyState=4:DoEvents:Loop为止
设置htmlColl2=ie2.document.getElementsByTagName(“td”)
对于HTMLCLL2中的每个htmlInput2
如果htmlInput2.className=“dxgv”,则
如果ActiveCell.Offset(RowCount).Value=”“,则
ActiveCell.Offset(RowCount).Value=htmlInput2.innerText
其他的
如果ActiveCell.Offset(RowCount).Value为“DELIVERED”,则
ActiveCell.Offset(行计数,-2)。Value=“”
其他的
ActiveCell.Offset(RowCount,-2).Value=htmlInput2.innerText
如果结束
退出
如果结束
如果结束
下一个htmlInput2
ie2.退出
设置shellWins=无
设置ie=无
设置ie2=无
RowCount=RowCount+1
环
设置shellWins=无
设置ie=无
设置ie2=无
端接头
副秘书长(秒)
把t调暗为单身
t=定时器+1/2
直到t
而不是这个:
Set htmlColl = ieDOC.getElementsByTagName("a")
For Each htmlInput In htmlColl
If htmlInput.ID = "clickElement" Then
htmlInput.Click
Exit For
End If
Next htmlInput
您应该能够做到这一点:
ieDOC.getElementById("clickElement").Click
Id
在给定页面中应该是唯一的。我看到您在其他地方使用了getElementById
,所以这里不使用它有什么原因吗
我想问题可能在于:
ie.Quit
所以试着去评论一下。可能与加载新页面的位置有关(新窗口与新选项卡?)
如果您在获取正确的IE文档时遇到问题,请尝试以下操作:
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
' assign it to the return value and exit the loop
sURL = o.document.Location
On Error GoTo 0
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
此函数将返回一个与提供的URL匹配的IE窗口(即,其URL以传递的sLocation
字符串开头的第一个窗口),而不是:
Set htmlColl = ieDOC.getElementsByTagName("a")
For Each htmlInput In htmlColl
If htmlInput.ID = "clickElement" Then
htmlInput.Click
Exit For
End If
Next htmlInput
您应该能够做到这一点:
ieDOC.getElementById("clickElement").Click
Id
在给定页面中应该是唯一的。我看到您在其他地方使用了getElementById
,所以这里不使用它有什么原因吗
我想问题可能在于:
ie.Quit
所以试着去评论一下。可能与加载新页面的位置有关(新窗口与新选项卡?)
如果您在获取正确的IE文档时遇到问题,请尝试以下操作:
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
' assign it to the return value and exit the loop
sURL = o.document.Location
On Error GoTo 0
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
此函数将返回一个IE窗口