Domanda

Realizzo applicazioni VBA sia per Word che Excel, esiste un modo per accedere alla barra di avanzamento che a volte appare nella barra di stato di Office.

È stato utile?

Soluzione

Quanto segue simulerà una barra di avanzamento nella barra di stato di 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

Altri suggerimenti

Vorrei inoltre raccomandare di registrare lo stato corrente della StatusBar, quindi ripristinarlo al termine di tutto.

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

Non ho avuto accesso alla barra di avanzamento, ma in passato ho usato qualcosa del genere per posizionare il testo sullo stato dell'attività nella barra di stato ...

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, non c'è modo di riprodurre la linea blu di punti usati da Word & amp; Excel mostra i progressi verso il 100%, ad esempio quando si apre un file.

Ricordo che una volta ho visto del codice per replicarlo nella barra di stato, ma era complesso e non lo consiglierei, quando è abbastanza sufficiente invece dire "X% completo". nella barra di stato, usando Application.StatusBar.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top