Make form visible in Word with VBA

I have a VBA program in Word that takes some time to do its thing. To assuage the user that the program is working, I display a form with messages and a progress bar to show what is happening. All is well until I create a new document that will display the results of the VBA program. The progress bar form is visible only when the original document is displayed. I would like the form to be visible while VBA is writing and formatting the new document.
      I’ve tried hiding the form before creating the new document then I unhide the form, but it is visible only from the original document. I’ve tried turning Application.ScreenUpdating on and off to no avail.
      Is there any hope of a solution?
      --JRA in Priddis
JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

have you tried putting it on top (z order) or showing it modally?
JohnRobinAllenRetired professor of FrenchAuthor Commented:
The form is visible when one has the main document visible. The problem occurs when I make a new document. If the new document is displayed, the user does not see the original document or the progress bar on the form.  
     Showing the form as modal stops all other VBA activity, which gives other problems. I would then have to make a loop around all other activity to keep returning to the form to display it again. Putting it on top (z order) sounds promising but I have no idea about how to do that.
     I think the problem has no solution, so I should perhaps withdraw the question.
There are a couple of ways to accomplish this.  One is to use the SetWindowPos API.  The other is to open the new documents as invisible.  There may be a more ways, but this will do for now.  I tested these with a userform with three command buttons.  You will probably package the code in a different configuration.
Option Explicit

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
                                 ByVal hWndInsertAfter As Long, _
                                 ByVal X As Long, _
                                 ByVal y As Long, _
                                 ByVal cx As Long, _
                                 ByVal cy As Long, _
                                 ByVal wFlags As Long) As Long

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
                    ByVal lpClassName As String, _
                    ByVal lpWindowName As String) As Long
Dim oDoc As Document

Private Sub CommandButton1_Click()
    Const HWND_TOPMOST = -1
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Dim lngHwnd As Long
    Do While lngHwnd = 0
        lngHwnd = FindWindow("ThunderDFrame", "UserForm1")   'your userform name will likely be different
    Call SetWindowPos(lngHwnd, HWND_TOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE)    ' Or SWP_NOSIZE
End Sub

Private Sub CommandButton2_Click()
    Set oDoc = Application.Documents.Add(Visible:=False)
End Sub

Private Sub CommandButton3_Click()
End Sub

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JohnRobinAllenRetired professor of FrenchAuthor Commented:
I used the simplest method, making the new document invisible with "Visible:=False". That, of course, did not make the new document invisible, but it ensured that my form was visible and fully reporting on what the VBA was doing. At the end of its work there was no need to add code to make the new document visible, so that single argument, Visible:=False, solved my problem
     Thanks to Akimark, and thanks to EE in general for saving persons like me many hours of work.

     JRA in Priddis, Alberta
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I was a bit hasty in accepting that solution. The reason it appeared to work is that it never stopped showing the original document, so the form was always visible and all the output of the program replaced the content of the original document. I thought my problem was solved, but all it accomplished was to clobber my original document.
     I still then have no solution to the problem. I'll see if the setWindowsPos suggestion will work, but it is so far removed from anything I have ever done that I suspect it will be a dead end. Still, I'll give it a try.
     My apologies for anyone with the same problem who looks here for a solution.

It isn't clear where/how you are displaying the user form and what's its function(s) is/are.  The sample code I supplied did not include any code to show the user form.  I did that manually in my testing.  The buttons allowed me to perform some actions subsequent to the user form opening.

Be sure that you close any documents that you open.  Otherwise, you will run out of memory.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I found a solution to my problem and would therefore simply leave it here and still assign the credit to Aikimark.
     The solution is to hide the form before making the new document. That displays the output document. Then show the form again, which switches the visible document to the original, the one with the VBA code.
      From that point on, the original document is displayed along with the progression bar form.
       The code for that is rather simple. In the following, “DuBellayForm” is the progression bar form, and the DoEvents line is just to ensure that the form has time to show itself.
      Call OpenNewDoc

     I apologize for the trouble I have caused and again express my appreciation to Aikimark for his (or her) attention to this problem.
JRA in Priddis, Alberta
Chris BottomleySoftware Quality Lead EngineerCommented:

Appreciate you are in the process of closing this but for information I had an issue where I lost focus whilst generating a new document making my status form invisible ... sounding therefore like your experience.

My cure was to create a new instance of word, making that instance invisible ... my form then remains onscreen and I can update it whilst i CONTINUE TO DO WORK ON THE HIDDEN APPLICATION.

JohnRobinAllenRetired professor of FrenchAuthor Commented:
Excellent idea, Chris. Thanks for writing. Currently my solution works, but I can easily envisage it not working as things get more complicated, so I'll use your idea when that happens.
    --John Robin (JRA in Priddis)
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Thanks for the help, and thanks for reopening the question for further comments.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Word Processors

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.