Search code examples
vbams-worduserform

VBA - Updating a ProcessBar from module - Error 800100007


I want to call a process bar from the module main and update the bar from main. I have in my UserForm1:

  • Label5 with the max value
  • Label2 with the current value
  • ProgressBar1 with the current value from 0 to 100

the module code is:

Public Sub main()
    Dim i, max, k, dummy As Long
    Dim myUserForm As UserForm1

    Set myUserForm = New UserForm1
    max = 100
    myUserForm.Label5 = max
    myUserForm.Label2 = 0
    myUserForm.Show
    For i = 1 To max
        myUserForm.UpdateStatus i
        For k = 1 To 10000              'loop for time-consuming
            dummy = Sqr(k)
        Next
    Next
    Unload myUserForm
End Sub

The code in UserForm1 is:

Option Explicit
Public Sub UpdateStatus(i)
    Me.Label2 = i
    Me.ProgressBar1 = i
    DoEvents
End Sub

The UserForm1 shows up once and then I get in main at myUserForm.UpdateStatus i the error 800100007. I am using VBA Word. How can I make it work?


Solution

  • You need to change how you display the form:

    myUserForm.Show vbModeless
    

    Currently, the loop is not executing until you close the form at which point the form is not available and an error is generated.