1

当用户确实单击“刷新”/“全部刷新”按钮时,excel 似乎只是为工作簿中的每个(或选定的)QueryTable 调用刷新方法。但是,在这里听 QueryTable 的 BeforeRefresh 和 AfterRefresh 事件并没有真正帮助我,因为我需要在工作簿中的所有QueryTables (分别在所有选定的 QueryTables 之后)更新之后执行一些东西。

有没有办法做到这一点?也许它可能以某种方式听鼠标点击刷新按钮?

4

2 回答 2

1

实际上,这就是我最初打算实现这一目标的方式。但是这里有一个问题。假设 QueryTable 的总数为 10。假设用户刚刚选择了一个 QueryTable,然后按下“全部刷新”。因此,首先我的算法将检查所选 QueryTables 的数量为 1。因此,我的计算将在 1 次刷新后开始,这是错误的。

同时,我尝试访问功能区中的“刷新”按钮。但它没有成功。由于某种原因,我的代码没有做任何事情......

Public Class ThisAddIn
   Private Sub ThisAddIn_Startup() Handles Me.Startup
      AddHandler Globals.ThisAddIn.Application.WorkbookActivate, AddressOf OnWorkbookOpened
   End Sub

   Private refrBtn As Office.CommandBarButton

   Private Sub OnClick(ByVal Ctrl As Office.CommandBarButton, _
                  ByRef CancelDefault As Boolean)
      MsgBox("PLS WORK!")
   End Sub

   Private Sub OnWorkbookOpened(wb As Excel.Workbook)
      Try
         refrBtn = CType(wb.Application.CommandBars.FindControl(Id:=459), Office.CommandBarButton)
         AddHandler refrBtn.Click, AddressOf OnClick
      Catch ex As Exception
         MsgBox(ex.Message)
         MsgBox(ex.GetType)
         MsgBox(ex.StackTrace.ToString)
      End Try
   End Sub
End Class

编辑:我忘记了我的登录数据,所以我创建了一个新帐户

于 2013-07-24T22:16:52.803 回答
0

I'm not using VSTO but I would investigate:

  • Create a global variable (or workbook property) set to 0
  • In each AfterRefresh event call a procedure
  • This procedure increments the counter
  • When the counter reaches the total number of QueryTables (or the total selected number), do what you need to do and reset the counter to 0.

You might also check the Success argument each time, so that your code might only run if the selected QTs were all successfully updated.

于 2013-07-24T19:48:57.243 回答