0

I have a small programme that opens and closes a number of different word documents while it runs. It loads some documents from the web so it takes a little while and I'd prefer to let the user watch a little progress bar or at least have a message in a form telling them to wait.

I can't seem to be able to keep that form on top of all other Office windows, however.

I have no problem with the code for the actual progress bar, just keeping the damn thing on top while the code is opening and closing windows. I tried hiding the application but this seems to prevent some of the code from running.

Regardless of whether I have modal or modeless set the form goes behind the activewindow and when it occasionally shows on top it won't repaint.

I may have just missed a "stayontop" property or something?

Thanks

4

1 回答 1

1

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

如果这不是您的问题,您可以使用 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

我把它放在表单的代码模块中,它在移动其他应用程序时似乎可以工作;它保持在顶部。

于 2009-02-02T15:48:19.807 回答