[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Make form visible in Word with VBA

Posted on 2014-08-11
11
Medium Priority
?
758 Views
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
0
Comment
Question by:JohnRobinAllen
  • 6
  • 3
10 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40254873
have you tried putting it on top (z order) or showing it modally?
0
 

Author Comment

by:JohnRobinAllen
ID: 40256007
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.
     --JRA
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40256212
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
        DoEvents
    Loop
    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()
    oDoc.Close
End Sub

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JohnRobinAllen
ID: 40259338
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
0
 

Author Comment

by:JohnRobinAllen
ID: 40259424
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.
     JRA
0
 
LVL 46

Accepted Solution

by:
aikimark earned 1000 total points
ID: 40259443
John

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

Assisted Solution

by:JohnRobinAllen
JohnRobinAllen earned 0 total points
ID: 40259739
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.
      DuBellayForm.Hide
      Call OpenNewDoc
      DoEvents
      DuBellayForm.Show

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

Expert Comment

by:Chris Bottomley
ID: 40259914
John

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.

cHRIS
0
 

Author Comment

by:JohnRobinAllen
ID: 40260557
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)
0
 

Author Closing Comment

by:JohnRobinAllen
ID: 40267283
Thanks for the help, and thanks for reopening the question for further comments.
     JRA
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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