none
Create A progress bar for the execution of a Word MACRO RRS feed

  • Question

  • Hi Experts,

    I had a Word document with a list of tasks. There is a button (of one of these tasks) launching a macro allowing to compare and create Word documents.

    I would like to add to my script a progress bar or a windows pop-up displaying the progress of the macro.
    Could you please help me to implement that because I don't know exactly how to do that.

    My script:

    Private Sub SummaryReportButton_Click()
        Dim objDocA As Word.Document
        Dim objDocB As Word.Document
        Dim objDocC As Word.Document
     
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolderA As Scripting.Folder
        Dim objFolderB As Scripting.Folder
        Dim objFolderC As Scripting.Folder
     
        Dim colFilesA As Scripting.Files
        Dim objFileA As Scripting.File
     
        Dim i As Integer
        Dim j As Integer
     
        Set objFSO = New FileSystemObject
        Set objFolderA = objFSO.GetFolder(ChooseFolder("Choose the folder with the original documents", ThisDocument.Path))
        Set objFolderB = objFSO.GetFolder(ChooseFolder("Choose the folder with revised documents", ThisDocument.Path))
        Set objFolderC = objFSO.GetFolder(ChooseFolder("Choose the folder for the comparisons documents", ThisDocument.Path))
     
        Set colFilesA = objFolderA.Files
     
        'Turn off DisplayAlerts
        Application.DisplayAlerts = wdAlertsNone
     
        For Each objFileA In colFilesA
        If objFileA.Name Like "*.docx" Then
            Set objDocA = Documents.Open(objFolderA.Path & "\" & objFileA.Name)
            Set objDocB = Documents.Open(objFolderB.Path & "\" & objFileA.Name)
            Set objDocC = Application.CompareDocuments( _
                OriginalDocument:=objDocA, _
                RevisedDocument:=objDocB, _
                Destination:=wdCompareDestinationNew)
            objDocA.Close
            objDocB.Close
            On Error Resume Next
            Kill objFolderC.Path & "\" & objFileA.Name
            On Error GoTo 0
     
            'Turn off DisplayAlerts
            Application.DisplayAlerts = wdAlertsNone
     
            objDocC.SaveAs FileName:=objFolderC.Path & "\" & objFileA.Name
            Application.DisplayAlerts = wdAlertsNone
            objDocC.Close SaveChanges:=True
     
        End If
        Next objFileA
     
    End Sub
     
    Function ChooseFolder(strTitle As String, strPath As String) As String
        Dim fldr As FileDialog
        Dim sItem As String
     
     
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = strTitle
            .AllowMultiSelect = False
            .InitialFileName = strPath
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
     
    NextCode:
        ChooseFolder = sItem
        Set fldr = Nothing
    End Function


    Wednesday, December 20, 2017 11:42 AM

Answers

  • Hi webmaster57100,

    below is an example for displaying the progress bar.

    you need to use UserForm to display the progressbar.

    you need to add one userform with label and progressbar control.

    then you need to add one module.

    and then you need to place one button on your Word document to run this example.

    code on button click:

    Private Sub CommandButton1_Click()
    
    UserForm1.Show
    
    End Sub

    code in userform:

    Private Sub UserForm_Activate()
    
    code
    
    End Sub

    code in module:

    Sub code()
    
    Dim i As Integer, j As Integer, pctCompl As Single
    
    
    For i = 1 To 100
        For j = 1 To 1000
            debug.print j
        Next j
        pctCompl = i
        progress pctCompl
    Next i
    
    End Sub
    
    Sub progress(pctCompl As Single)
    
    UserForm1.Text.Caption = pctCompl & "% Completed"
    UserForm1.Bar.Width = pctCompl * 2
    
    DoEvents
    
    End Sub

    Output:

    this is just a demo example to show how to implement progressbar.

    further you need to modify this code and merge it with your process to display the progress of your process.

    other examples that may give you some extra information:

    How to use a Progress bar for Macros

    Show Progress from VBA

    Implementing a Progress Bar in Word VBA

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by webmaster57100 Thursday, December 21, 2017 6:34 AM
    Thursday, December 21, 2017 3:03 AM
    Moderator