Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Userform retains focus while procedure runs 5

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
I have created a userform that includes a progress bar. How do I keep this userform in the foreground while a procedure which is opening and closing documents runs?

I know you can keep a msgbox in the foreground with vbMsgBoxSetForeground. Is this possible with a userform?

Thanks!
 
TonyJollans said:
you probably don't see much effect on your progress bar from the loop you posted

:) In reality, I don't see any effect on my progress bar from the loop I posted because I can't see my progress bar!! :)

I understand my progress bar code may be incorrect, yet I have not even been able to test it yet due to trying to figure out how to make that progress bar even be correctly visible.

I have Application.Visible = False (while the process is running). When I make the progress bar modal, it shows fine but obviously nothing happens, since it is modal. When I make it modeless, the process runs fine, yet the progress bar 'userform' is white (blank).

As you can see, I have not even gotten to the point to where I can test my progress bar code, because I can't even view the progress bar currently. If I can get that worked out, then I can move on to correcting my progress bar code. :)
 
RP1America said:
I have Application.Visible = False (while the process is running).

That'll be why you can't see the UserForm (or anything else that belongs to the application window).

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Tony said:
That'll be why you can't see the UserForm (or anything else that belongs to the application window).
That's not true. Visibility of the userform does not depend on visibility of host application.

RP1America, try to debug your code (without hiding the application) and see what's going on. Do you have errors switched off? You try assign string to label's width (Me.Label1.Width = Format(Me.Label1.Width + sIncrement, "#.##")) I get type mismatch here.

combo
 
Actually, we can achieve pretty much what RP1America wants
the first userform's 'OK' click event executes the opening, populating of bookmark text, saving file as, and closing 246 Word documents as well as shows the second userform - progress/abort)

1) userform2, which contains a progress bar and a command button titled: abort, shows and retains focus throughout process
2) progress bar works properly.
3) abort button to be programmed so that the process stops if the user presses it.
with some minor modification of the code given in thread707-1574371

Ok - UserForm1. One command button
Code:
[blue]Option Explicit

Private Sub CommandButton1_Click()
    ProgressForm.Show False
    Me.hide
    ExampleWorkLoop
    Me.Show False
End Sub

Private Sub ExampleWorkLoop()
    Dim starttime As Single
    
    CancelRoutine = False
    ProgressForm.UpdateProgress 0, 0, 10
    starttime = Timer
    
    Do
        [green]' This is your processing loop. In my example we're just looping
        ' whilst a timer runs. In RP1America's case this should be the loop
        ' processing the 247 (or whatever) documents[/green]
        If Not ProgressForm Is Nothing Then ProgressForm.UpdateProgress CSng(Timer - starttime)
    Loop Until Timer - starttime >= 10 Or CancelRoutine [green]' Run for 10 seconds or until cancelled
    
    ' For sake of example show how we terminated the loop?[/green]
    MsgBox IIf(CancelRoutine, "Cancelled", "Timed out")
    If Not ProgressForm Is Nothing Then ProgressForm.ProgressDone
End Sub[/blue]
As you can see the ExampleCode procedure is just the ExampleCode from the previous thread with minor additions to deal with updating a progress form.

And here is the progress form, called ProgressForm, with one command button and a progressbar
Code:
[blue]Option Explicit

Private Sub cmdCancel_Click()
    CancelRoutine = True
    ProgressDone
End Sub

Public Sub UpdateProgress(Current As Single, Optional Min As Variant, Optional Max As Variant)
    If Not IsMissing(Min) Then ProgressBar1.Min = Min
    If Not IsMissing(Max) Then ProgressBar1.Max = Max
    ProgressBar1.Value = Current
    DoEvents
End Sub

Private Sub UserForm_Initialize()
    ProgressBar1.Min = 0
    ProgressBar1.Max = 100
End Sub

Public Sub ProgressDone()
    Unload Me [green]' Me.Hide might be a reasonable choice here as well[/green]
End Sub[/blue]

And the module presented in that other thread now simply contains:
Code:
[blue]Option Explicit

Public CancelRoutine As Boolean [green]' Alternatively, could be a public property of UserForm1 rather than the global we have chosen to use here[/green]

Public Sub Main()
    UserForm1.Show False
End Sub[/blue]
It is important that you start the example via Main - which will be the only Macro showing in Word for this example

Whilst superficially similar to Tony's code this version is closer to RP1America's earlier specifications. As to whether those specifications are a good idea ... ;-)



 
combo said:
tony said:
That'll be why you can't see the UserForm (or anything else that belongs to the application window).
That's not true. Visibility of the userform does not depend on visibility of host application.

Sorry - my mistake, you are correct. Although changing the application's visibility does have some effect on userform visibility, it is not dependent, and the two can be different.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
I simply do not see why there is a requirement for a separate userform to hold the progressbar. I don't.

I resize the original userform. In its original state, the progressbar is not visible, nor the Abort button. Once the action button is clicked, the userform is resized with all controls becoming invisible, but the progressbar and Abort button now Visible. The userform is resized to show ONLY the progressbar and Abort button.

Thus, all controls are on ONE userform, and thus all controls are in Scope.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
>I simply do not see why there is a requirement for a separate userform to hold the progressbar. I don't

As I said "As to whether those specifications are a good idea ..."


However, just as a thought - what if I have multiple occassions when I wanted to show a progress bar, not just one. With my version you just have to have the one progressbar form with no dependencies on anything else, and can happily reuse it as many times as and from wherever you like. You can even drop it into other projects and it still works with no side effects ...
 
I have a separate userform for just this reason - I need to call it in multiple places (including times before the main userform has loaded).

It has public properties for min, max, and value, as well as caption and title for the form. I call that form, set a few properties, and set it's value when it needs to update.
 
All good points, and I am always in favour of anything re-useable!

strongm: "As I said "As to whether those specifications are a good idea ...""

Yes, that is why I made my comment, because, while I am not totally understanding of what is going on, I remain rather leery of the multiple (and hard-coded) aspect of what seems to be going on.

I do think this is a good and worthwhile thread though.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
>It has public properties for min, max, and value,

Yep, if I was doing this properly instead of as a quick example I'd add properties for those to complement the UpdateProgress method
 
Okay, I have taken fumei's advice and removed the second userform, and added the label and abort button to the original userform. I have adjusted my code, yet I am still missing something. I am guessing that instead of a For Each loop, I may need a Do While? Not really sure, just guessing.

Currently with the code as is, the folders are created, but nothing else happens. What am I missing?

Thanks!

Code:
Private Sub cmdCreate_click()

Application.ScreenUpdating = False
Application.Visible = False

'create "out" folders
Call CreateFolders

Label1.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Label10.Visible = False
Label11.Visible = False
Label12.Visible = False
Label13.Visible = False
Label14.Visible = False
txtYear.Visible = False
cboQuarter.Visible = False
txtRT11.Visible = False
txtRT12.Visible = False
txtRT13.Visible = False
txtRT14.Visible = False
txtRT21.Visible = False
txtRT22.Visible = False
txtRT23.Visible = False
txtRT24.Visible = False
cmdCreate.Visible = False
cmdClear.Visible = False
cmdCancel.Visible = False

Label15.Visible = True
Label16.Visible = True
cmdAbort = True

Dim lDocCount As Long
Dim i As Integer
Dim doc As Document
Dim lMaxProgressBarWidth As Long
Dim sIncrement As Single

' Resize the UserForm
Me.Width = 240
Me.Height = 120

' Resize the label
Me.Label16.Caption = ""
Me.Label16.Width = 0
Me.Label16.BackColor = wdColorBlue

lMaxProgressBarWidth = 200
lDocCount = 287
sIncrement = lMaxProgressBarWidth / lDocCount
i = 1

For Each doc In Documents
    Me.Label16.Width = Format(Me.Label16.Width + sIncrement, "#.##")
    Me.Caption = "Creating " & CStr(i) & " of " & CStr(lDocCount)
    Me.Repaint

i = i + 1
Next doc


'open, merge, save, close all docs
Call Proposal1
Call Proposal2
Call Proposal3
Call Proposal4
Call Proposal5
Call Proposal6

Application.Visible = True

MsgBox "File Creation Complete.", vbOKOnly, "Complete"

'close document if more Word documents are open, otherwise close Word application
If Documents.Count > 1 Then
   '  more than one doc open
   ActiveDocument.Close
Else  ' count = 1
   ActiveDocument.Close
   Application.Quit
End If


End Sub
 
just thinking

Code:
For Each doc In Documents
    Me.Label16.Width = Format(Me.Label16.Width + sIncrement, "#.##")
    Me.Caption = "Creating " & CStr(i) & " of " & CStr(lDocCount)
    Me.Repaint

i = i + 1
Next doc

Inside this loop you have to do something or it just does a loop and does actually nothing but move your progress bar.

Does your
call proposal statements need to be inside the loop?
Are these procedures based off 1 file if so how do they know what file? Do you need to send them a variable?

Your loop should do something with each file. There for creating a progress bar based off of completion of action on 1 file.

Stepping through codes using f8 helps alot to see what is going on.

Hope this helps and makes sense.



ck1999
 
And of course part of the problem is:
Code:
For Each doc In Documents
as you are opening and doing the stuff to each document, you are also...saving and closing that document.

Therefore that document is no longer IN Documents.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
It's important to keep learning. It keeps a brain active and flexible...
I remember reading an article once in In-Fisherman. It was about electricity and fishing and had to do with putting a small charge on a steel downrigger line. It was one of the great fishing articles that I ever read. Then at the end, it explained that the whole thing was only applicable to salt water fishing and I'm in Michigan. Didn't matter. It was a great article. This is a great thread. I don't know if I'll ever use any of this but stars for everyone for the thought that went into this.
(Actually, everyone except Mark Walsh. When he posts the code for his resuable progress bar he'll get a star as well.)

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top