访问Office应用程序状态栏中的进度条

5

我为Word和Excel构建VBA应用程序,是否有办法访问在Office状态栏中出现的进度条。

4个回答

4
以下是在Excel状态栏中模拟进度条的示例代码:
Public Sub UpdateStatusBar(percent As Double, Optional Message As String = "")

    Const maxBars As Long = 20
    Const before As String = "["
    Const after As String = "]"

    Dim bar As String
    Dim notBar As String
    Dim numBars As Long

    bar = Chr(31)
    notBar = Chr(151)
    numBars = percent * maxBars

    Application.StatusBar = _
    before & Application.Rept(bar, numBars) & Application.Rept(notBar, maxBars - numBars) & after & " " & _
         Message & " (" & PercentageToString(percent) & "%)"

    DoEvents

End Sub

3
我建议在此基础上,记录当前状态栏的状态,并在所有操作完成后恢复原状。
Dim OldStatus
With Application
    OldStatus = .DisplayStatusBar
    .DisplayStatusBar = True
    .StatusBar = "Doing my duty, please wait..."
End With
' Do what you do best here (you can refresh the .StatusBar message with updted, as needed)
With Application
    .StatusBar = False
    .DisplayStatusBar = OldStatus
End With

0

我没有访问进度条,但过去我使用类似的方法将任务状态文本放置在状态栏中...

Sub StatusBarExample()
    Application.ScreenUpdating = False 
    ' turns off screen updating
    Application.DisplayStatusBar = True 
    ' makes sure that the statusbar is visible
    Application.StatusBar = "Please wait while performing task 1..."
    ' add some code for task 1 that replaces the next sentence
    Application.Wait Now + TimeValue("00:00:02")
    Application.StatusBar = "Please wait while performing task 2..."
    ' add some code for task 2 that replaces the next sentence
    Application.Wait Now + TimeValue("00:00:02")
    Application.StatusBar = False 
    ' gives control of the statusbar back to the programme
End Sub

0
据我所知,没有办法复制 Word 和 Excel 使用的蓝色点线来显示进度达到100%,例如在打开文件时。
我记得曾经看到过一些代码来在状态栏中复制它,但是它很复杂,而且我不建议这样做,因为使用 Application.StatusBar 在状态栏中说“X%完成”就足够了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接