使用任务调度程序刷新Excel查询
我正在尝试在 MS Office Professional Plus 2016 中自动化查询刷新。
我有一个运行VBS脚本的CMD脚本,该脚本运行Excel宏。如果我手动运行它,一切都可以。当我设置Windows 任务调度程序并选择选项时,就会发生问题。
我的宏将查询结果日志保存到文本文件,以便我可以确定代码断开的位置。在我看来,Excel在使用任务调度程序运行时会显示一个警报框(或类似的内容)。由于调度程序隐藏了所有警报,因此我无法确定用户的期望。如果我手动或通过任务调度程序运行“仅在用户登录”时,没有警报/提示。
这是我的Refreshqueries()sub。我尝试评论代码,并确认打破整个自动化的行是.refresh
withable.querytable 。
Private Sub RefreshQueries()
AddToLogFile ("Hello from subroutine RefreshQueries().")
Dim iWorksheet As Excel.Worksheet
Dim iTable As Excel.ListObject
'Check each worksheet.
For Each iWorksheet In Excel.ActiveWorkbook.Worksheets
AddToLogFile ("For-loop for iWorksheet " & iWorksheet.Name)
'Check all Objects if it is a query object.
For Each iTable In iWorksheet.ListObjects
If iTable.SourceType = Excel.XlListObjectSourceType.xlSrcQuery Then
AddToLogFile ("Trying to refresh iTable: " & iTable.Name)
QueryTimeStart = Timer
On Error Resume Next
With iTable.QueryTable 'Refresh the query data.
.BackgroundQuery = False
.EnableRefresh = True
.Refresh
End With
If Err.Number <> 0 Then
QueryRunTime = CalculateRunTime("QueryRunTime") 'Stop timer and get the duration.
Call AddToHtmlErrorTable(iTable.Name, Err.Number, Err.Description, QueryRunTime) 'Add entry to error table.
AddToLogFile ("Query in iTable " & iTable.Name & " failed. Description: " & Err.Description)
NumberOfFailedQueries = NumberOfFailedQueries + 1 'IMPORTANT: increment must be after updating html error table!
Err.Clear 'Clear errors between for loops.
Else
NumberOfSuccessfulQueries = NumberOfSuccessfulQueries + 1
AddToLogFile ("Query in iTable " & iTable.Name & " successfully refreshed.")
End If
End If
Next iTable
Next iWorksheet
AddToLogFile ("Exiting subroutine RefreshQueries().")
End Sub
我猜我的问题如下:
- 我们可以以某种方式抓住Excel在后台显示的内容(如果我手动运行它没有弹出),或者
- 我们可以自动确认Excel中的任何显示的消息(不知道它是什么),或者
- 是否有任何已知的设置可以执行连接而没有任何确认。
有人对此问题有想法,经验或建议吗?
I am trying to automate Query refresh in MS Office Professional Plus 2016.
I have a cmd script which runs vbs script which runs Excel macro. Everything works if I run it manually. The problem occurs when I set up Windows Task Scheduler and select the option "run whether user is logged on or not".
My macro is saving query result log to text file so I can determine where the code breaks. Looks to me that Excel displays an alert box (or something similar) when running with Task Scheduler. I can not determine what is expected from user since the scheduler hides all alerts. There are no alerts/prompts if I run the cmd script manually or via Task Scheduler with option "run only if user is logged on".
Here is my RefreshQueries() sub. I tried commenting the code and confirmed that line that breaks the whole automation is .Refresh
inside With iTable.QueryTable
.
Private Sub RefreshQueries()
AddToLogFile ("Hello from subroutine RefreshQueries().")
Dim iWorksheet As Excel.Worksheet
Dim iTable As Excel.ListObject
'Check each worksheet.
For Each iWorksheet In Excel.ActiveWorkbook.Worksheets
AddToLogFile ("For-loop for iWorksheet " & iWorksheet.Name)
'Check all Objects if it is a query object.
For Each iTable In iWorksheet.ListObjects
If iTable.SourceType = Excel.XlListObjectSourceType.xlSrcQuery Then
AddToLogFile ("Trying to refresh iTable: " & iTable.Name)
QueryTimeStart = Timer
On Error Resume Next
With iTable.QueryTable 'Refresh the query data.
.BackgroundQuery = False
.EnableRefresh = True
.Refresh
End With
If Err.Number <> 0 Then
QueryRunTime = CalculateRunTime("QueryRunTime") 'Stop timer and get the duration.
Call AddToHtmlErrorTable(iTable.Name, Err.Number, Err.Description, QueryRunTime) 'Add entry to error table.
AddToLogFile ("Query in iTable " & iTable.Name & " failed. Description: " & Err.Description)
NumberOfFailedQueries = NumberOfFailedQueries + 1 'IMPORTANT: increment must be after updating html error table!
Err.Clear 'Clear errors between for loops.
Else
NumberOfSuccessfulQueries = NumberOfSuccessfulQueries + 1
AddToLogFile ("Query in iTable " & iTable.Name & " successfully refreshed.")
End If
End If
Next iTable
Next iWorksheet
AddToLogFile ("Exiting subroutine RefreshQueries().")
End Sub
I guess my question is as follows:
- can we somehow catch what prompt Excel is showing in the background (nothing pops up if I run it manually), or
- can we confirm any shown message in Excel automatically (without knowing what it is), or
- are there any known settings which would execute the connection without any confirmation.
Does anyone have an idea, experience, or suggestion regarding this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在VBA例程中添加错误捕捉器“ rel =“ nofollow noreferrer”>在这里
You need to add error catcher to your VBA routine like described here