使用任务调度程序刷新Excel查询

发布于 2025-02-05 18:57:03 字数 2458 浏览 2 评论 0原文

我正在尝试在 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

我猜我的问题如下:

  1. 我们可以以某种方式抓住Excel在后台显示的内容(如果我手动运行它没有弹出),或者
  2. 我们可以自动确认Excel中的任何显示的消息(不知道它是什么),或者
  3. 是否有任何已知的设置可以执行连接而没有任何确认。

有人对此问题有想法,经验或建议吗?

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:

  1. can we somehow catch what prompt Excel is showing in the background (nothing pops up if I run it manually), or
  2. can we confirm any shown message in Excel automatically (without knowing what it is), or
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2025-02-12 18:57:04

您需要在VBA例程中添加错误捕捉器“ rel =“ nofollow noreferrer”>在这里

Private Sub RefreshQueries()
On Error Goto MyError

' .... All your code

Exit sub
MyError: 
'Do your magic here with Err.object to log the event or whatever
AddToLogFile ("#Error in RefreshQueries().:" & Err.Discription)
Resume Next
End Sub   

You need to add error catcher to your VBA routine like described here

Private Sub RefreshQueries()
On Error Goto MyError

' .... All your code

Exit sub
MyError: 
'Do your magic here with Err.object to log the event or whatever
AddToLogFile ("#Error in RefreshQueries().:" & Err.Discription)
Resume Next
End Sub   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文