使用Office VBA的进度条

时间:2009-02-01 22:26:57

标签: vba ms-word progress-bar ms-office word-vba

我有一个小程序,可以在运行时打开和关闭许多不同的word文档。它从网上加载一些文档,所以需要一段时间,我宁愿让用户观看一个小进度条,或者至少有一个表单中的消息告诉他们等待。

但是,我似乎无法将该表单保存在所有其他Office窗口之上。

我对实际进度条的代码没有任何问题,只是在代码打开和关闭窗口时将该死的东西保持在顶部。我试图隐藏应用程序,但这似乎阻止了一些代码运行。

无论我是否有模态或无模式设置,表单都会在活动窗口后面,当它偶尔显示在顶部时,它将不会重新绘制。

我可能刚刚错过了“stayontop”财产或什么?

由于

1 个答案:

答案 0 :(得分:1)

我认为没有任何内置的方法可以让表单在VBA中保持最佳状态,但有一个问题是,当您更新表单上的任何内容时,您是否在调用DoEvents?我的经验是,除非你在循环中点击Next语句之前调用DoEvents,否则表单不会重新绘制。

如果这不是您的问题,您可以使用Windows API调用将窗口置于顶部,但我不确定它是否使用此代码保持在顶部:

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long

Const SWP_NOMOVE = 2
Const SWP_NOSIZE As Long = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2

Private Declare Function SetWindowPos Lib "user32" _
      (ByVal hwnd As Long, _
      ByVal hWndInsertAfter As Long, _
      ByVal x As Long, _
      ByVal y As Long, _
      ByVal cx As Long, _
      ByVal cy As Long, _
      ByVal wFlags As Long) As Long

Private Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) As Long

   If Topmost = True Then 'Make the window topmost
      SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
   Else
      SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
      SetTopMostWindow = False
   End If
End Function

Private Function GetFormHwnd() As Long
    GetFormHwnd = FindWindow(CLng(0), Me.Caption)
End Function

Public Sub SetFormAsTopMostWindow()
    Call SetTopMostWindow(GetFormHwnd(), True)
End Sub

我把它放在一个表单的代码模块中,它似乎在移动其他应用程序时起作用;它保持在最顶层。