Make form visible in Word with VBA

Posted on 2014-08-11
Last Modified: 2014-08-18
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
Question by:JohnRobinAllen
    LVL 44

    Expert Comment

    have you tried putting it on top (z order) or showing it modally?

    Author Comment

    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.
    LVL 44

    Expert Comment

    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


    Author Comment

    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

    Author Comment

    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.
    LVL 44

    Accepted 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.

    Assisted Solution

    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
    LVL 59

    Expert Comment

    by:Chris Bottomley

    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.


    Author Comment

    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)

    Author Closing Comment

    Thanks for the help, and thanks for reopening the question for further comments.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Adobe Customization Wizard XI issues 26 137
    PDF to Word 25 81
    All doc, xls, and pdf's are gone 9 60
    base64 decode encode 12 55
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now