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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/217816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 09:59:41  来源:igfitidea点击:

Accessing the progress bar in an Office application's status bar

excelvbams-wordprogress-barms-office

提问by JonnyGold

I build VBA applications for both Word and Excel, is there any way to access the progress bar that sometimes appears in the Office status bar.

我为 Word 和 Excel 构建了 VBA 应用程序,有什么方法可以访问有时出现在 Office 状态栏中的进度条。

采纳答案by Carl G

The following will simulate a progress bar in Excel's status bar:

以下将模拟 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

回答by KnomDeGuerre

I would recommend in addition, to record the current state of the StatusBar, then restore it when everything is done.

此外,我还建议记录 StatusBar 的当前状态,然后在一切完成后恢复它。

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

回答by dbb

AFAIK, there is no way to reproduce the blue line of dots used by Word & Excel to show progress towards 100%, eg when opening a file.

AFAIK,无法重现 Word 和 Excel 使用的蓝色点线来显示 100% 的进度,例如在打开文件时。

I remember once seeing some code to replicate it in the status bar, but it was complex, and I wouldn't recommend it, when it is quite sufficient instead to say "X% complete" in the status bar, using Application.StatusBar.

我记得曾经在状态栏中看到一些代码来复制它,但它很复杂,我不推荐它,当使用 Application.StatusBar 在状态栏中说“X% 完成”就足够了。

回答by Galwegian

I have not accessed the progress bar, but I have in the past used something like this to place task status text in the status bar...

我没有访问进度条,但我过去曾使用过类似这样的东西在状态栏中放置任务状态文本......

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