Acceso a la barra de progreso en la barra de estado de una aplicación de Office
-
03-07-2019 - |
Pregunta
Construyo aplicaciones VBA para Word y Excel, ¿hay alguna forma de acceder a la barra de progreso que a veces aparece en la barra de estado de Office.
Solución
Lo siguiente simulará una barra de progreso en la barra de estado de 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
Otros consejos
Recomendaría además, registrar el estado actual de la Barra de estado, y luego restaurarlo cuando todo esté listo.
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
No he accedido a la barra de progreso, pero en el pasado he usado algo como esto para colocar el texto de estado de la tarea en la barra de estado ...
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
AFAIK, no hay forma de reproducir la línea azul de puntos que usa Word & amp; Excel para mostrar el progreso hacia el 100%, por ejemplo, al abrir un archivo.
Recuerdo que una vez vi un código para replicarlo en la barra de estado, pero era complejo, y no lo recomendaría, cuando es suficiente en lugar de decir " X% completado " en la barra de estado, usando Application.StatusBar.