PDA

View Full Version : [SOLVED:] Progress Bar



kattai
11-29-2022, 06:50 AM
I want to insert a progress bar in my Word VBA program macro at a specific (RowNumber, ColumnNumbar and ParaNumber) location. The value of the progress bar will be 25% or 50% or 75% or 100%. The label for the progress bar will be inserted within a frame. Both the label and the frame need to have a some background color.
I will appreciate if someone can help me to get started with a sample code that I can work around to move ahead. Thanks

Chas Kenyon
11-29-2022, 06:54 AM
Start with an Internet search like https://duckduckgo.com/?q=vba+progress+bar+while+macro+is+running&t=newext&atb=v248-1&ia=web.

kattai
11-29-2022, 07:45 AM
Hi Chas, Thanks for your reply. All the links, sent by you, point to examples that show the progress on a USERFORM when the macro is running. I do not want to create a UserForm since I want the progress bar to appear at a specific location in my word Table (RowNumber, ColumnNumber, ParaNUmber). The value of the progress bar is fixed at 25%/ 50%/ 75%/ 100%. It will be static as opposed to the dynamic progress shown on the threads forwarded by you. It will be helpful if there is a sample code.

macropod
11-29-2022, 03:23 PM
For a true progress bar, you need a userform. The other option is to output the progress to the StatusBar. See, for example:
Mail merge to individual files, there's any kind of progress indicator (msofficeforums.com) (https://www.msofficeforums.com/word-vba/40221-mail-merge-individual-files-theres-kind-progress.html)
convert text numbers to real numbers, partial Cell deleting, deleting rows between se (vbaexpress.com) (http://www.vbaexpress.com/forum/showthread.php?34031-convert-text-numbers-to-real-numbers-partial-Cell-deleting-deleting-rows-between-se&highlight=sbar)
Retrieve the Font Type and Size Using VBA (vbaexpress.com) (http://www.vbaexpress.com/forum/showthread.php?26814-Retrieve-the-Font-Type-and-Size-Using-VBA&highlight=sbar)
Exporting tracked changes and comments to excel (msofficeforums.com) (https://www.msofficeforums.com/word-vba/40314-exporting-tracked-changes-comments-excel.html)
Regardless, the output must be refreshed on each iteration, as it is volatile. In any event, your code needs to calculate what % has been reached on each iteration.

gmayor
11-29-2022, 10:29 PM
Fellow contributors are right about progress bars, but you may be able to simulate a progress bar in your table by embedding a four cell table in one of the cells, then call the following macro to colour the cells according to a value that you wish to log. In the example code, the cells are coloured according to the value in the calling macro - here 50

Option Explicit

Sub Macro1()
'call the bar
ProgressBar 50
End Sub

Sub ProgressBar(lngVal As Long)
'Graham Mayor - https://www.gmayor.com - Last updated - 30 Nov 2022
Dim oTable As Table
Dim oNestedTable As Table
Dim oCell1 As Cell, oCell2 As Cell, oCell3 As Cell, oCell4 As Cell
Set oTable = ActiveDocument.Tables(1)
For Each oNestedTable In oTable.Tables
Set oCell1 = oNestedTable.Range.Cells(1)
Set oCell2 = oNestedTable.Range.Cells(2)
Set oCell3 = oNestedTable.Range.Cells(3)
Set oCell4 = oNestedTable.Range.Cells(4)
Select Case True
Case Is = lngVal = 100
MsgBox "100"
oCell4.Shading.BackgroundPatternColor = wdColorGreen
oCell3.Shading.BackgroundPatternColor = wdColorGreen
oCell2.Shading.BackgroundPatternColor = wdColorGreen
oCell1.Shading.BackgroundPatternColor = wdColorGreen
Case Is = lngVal >= 75 And lngVal < 100
oCell4.Shading.BackgroundPatternColor = wdColorWhite
oCell3.Shading.BackgroundPatternColor = wdColorGreen
oCell2.Shading.BackgroundPatternColor = wdColorGreen
oCell1.Shading.BackgroundPatternColor = wdColorGreen
Case Is = lngVal >= 50 And lngVal < 75
oCell4.Shading.BackgroundPatternColor = wdColorWhite
oCell3.Shading.BackgroundPatternColor = wdColorWhite
oCell2.Shading.BackgroundPatternColor = wdColorGreen
oCell1.Shading.BackgroundPatternColor = wdColorGreen
Case Is = lngVal >= 25 And lngVal < 50
oCell4.Shading.BackgroundPatternColor = wdColorWhite
oCell3.Shading.BackgroundPatternColor = wdColorWhite
oCell2.Shading.BackgroundPatternColor = wdColorWhite
oCell1.Shading.BackgroundPatternColor = wdColorGreen
Case Is = lngVal >= 0 And lngVal < 25
oCell4.Shading.BackgroundPatternColor = wdColorWhite
oCell3.Shading.BackgroundPatternColor = wdColorWhite
oCell2.Shading.BackgroundPatternColor = wdColorWhite
oCell1.Shading.BackgroundPatternColor = wdColorWhite
End Select
Next oNestedTable
lbl_Exit:
Set oTable = Nothing
Set oNestedTable = Nothing
Set oCell1 = Nothing
Set oCell2 = Nothing
Set oCell3 = Nothing
Set oCell4 = Nothing
Exit Sub
End Sub

kattai
12-09-2022, 12:32 AM
Thank you very much for your suggestion. I could work around to get my solution