Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 08-11-2014, 12:51 PM
Cosmo Cosmo is offline Windows Vista Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 195
Cosmo is on a distinguished road
Default Create new invisible document, showing progress bar

I would like to create a new invisible document, show a progress bar userform while the document is being processed, then show the finished document only once it is finished.

Everything works properly to create the content, but looks a little clunky: when the document is created, I get an empty document window (I would like this to not show up at all until finished), and my progressbar userform shows up blank while the process is running (the title is visible, but the progressbar and text labels within are not displayed).

I have tried setting 'Application.ScreenUpdating = False' at various points during the process (i.e. moving it before/after the document is created, or the userform is shown), but I still end up with a blank window while the document is processing. I have tried altering the sequence, but if I show the progressbar form before the document is created, it gets hidden by the document window.

Does anyone know how I can get the new document to be hidden while the rest of the processing is occurring? I can lose the progressbar if necessary, but I would prefer to let the user know that something is still going on.
Reply With Quote
  #2  
Old 08-11-2014, 09:54 PM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 2,165
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

Where are you running the macro from that creates the document?

If you run it from Word, then you are going to have an empty Word window, or the document present that you are running it from.

As for the progress indicator, you can show the form modeless and it will remain displayed while stuff happens, but you are going to have to provide updates to that display in order for the bar to change.

You can download a progress bar userform from the link, and use the following code to update it.

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Macro1()
Dim oFrm As frmProgress
Dim PartDone As Double
Dim i As Long
Set oFrm = New frmProgress
oFrm.Show vbModeless
Application.ScreenUpdating = False
'Below is the part that reflects the updating of the form - here a simple loop
For i = 1 To 10
Sleep 1000 ' a one second delay
PartDone = i / 10
oFrm.lblProgress.Width = oFrm.fmeProgress.Width * PartDone
oFrm.Caption = "Processing item " & i & " of " & 10
DoEvents

Next i
Unload oFrm
End Sub
Reply With Quote
  #3  
Old 08-12-2014, 05:15 AM
Cosmo Cosmo is offline Windows Vista Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 195
Cosmo is on a distinguished road
Default

Thanks for the response. I already have the progressbar form working properly, but it doesn't display as anything other than a blank window:
Progressbar.jpg
The progressbar control and the label control which are on the form don't show at all.

So, there is no way to create a new invisible document from within Word itself?
Reply With Quote
  #4  
Old 08-12-2014, 06:41 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 2,165
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

If the progress bar doesn't display anything, then clearly it is not working. The idea is that it is updated by processes in the macro that calls it. This is usually in the form of a loop, as in the example I posted earlier. You need to establish how many updates to make and then increment a counter at those update points and use the counter to calculate the value of the bar at those update points. The following example shows how to do that with 10 steps (though as the macro is a simple one, I have added delays of 1 second before each update). The macro, run from Word, does not display the document until the macro is finished.


Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Macro1()
Dim oDoc As Document
Dim oFrm As frmProgress

    Set oDoc = Documents.Add(Visible:=False)

    Set oFrm = New frmProgress
    oFrm.Show vbModeless
    Application.ScreenUpdating = False

    UpdateBar oFrm, 1, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 2, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 3, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 4, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 5, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 6, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 7, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 8, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 9, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Sleep 1000        ' a one second delay
    UpdateBar oFrm, 10, 10
    oDoc.Range.InsertAfter "A line of text" & vbCr
    Unload oFrm
    oDoc.ActiveWindow.Visible = True
End Sub

Sub UpdateBar(oFrm As frmProgress, Count As Long, TotalSteps As Long)
Dim PartDone As Double

    PartDone = Count / TotalSteps
    oFrm.lblProgress.Width = oFrm.fmeProgress.Width * PartDone
    oFrm.Caption = "Processing item " & Count & " of " & TotalSteps
    DoEvents
End Sub
Reply With Quote
  #5  
Old 08-12-2014, 06:47 AM
Cosmo Cosmo is offline Windows Vista Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 195
Cosmo is on a distinguished road
Default

Thanks, I'll check that to see if it works in my project. I have used my progressbar form before with no problems, not sure if there's something with the creation of my document which is causing the problems. I wasn't sure if setting the screenUpdating to false was preventing the form from updating, but I wasn't able to find the correct order of events to make it work.
Reply With Quote
  #6  
Old 01-05-2017, 05:46 PM
wlcdo2 wlcdo2 is offline Windows 7 32bit Office 2013
Novice
 
Join Date: Jun 2016
Posts: 17
wlcdo2 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You can download a progress bar userform from the link, and use the following code to update it.
Hello Graham,
I came across your post here which was very valuable for my project. Thank you. I did try the link to download but I kept getting a 404 error. Wasn't a deal breaker for me because I managed to create a Progress Bar version based on the additional code you had also posted in the same thread. Just wondered if you knew the link wasn't working? or maybe it's at my end?

Secondly, when I call my Progress Bar to display from Document_Open, it seems to run after Word has finished loading all other attributes of the document (i.e. ActiveX Controls, Shapes & Images, etc). I've tried calling it from Document_Open located in ThisDocument and also AutoOpen from a standard Module. My document takes perhaps 10 - 15 secs to open so I was hoping to display the Progress Bar at this time; Progress Bar is updated simply based on time interval as per your example. I'm currently using Word 2016 - if that matters?

Thirdly, I had to change Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) to Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) as I'm running a 64 bit system. Is there a way to use VBA to check whether 32 or 64 bit?

I'm still reasonably new to using these forums so I hope this isn't out of line & is is OK to ask?

Kind regards, Corin.
Reply With Quote
  #7  
Old 01-05-2017, 08:22 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,326
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You could try something along the lines of:
Code:
Dim wdDoc As Document
Set wdDoc = Documents.Add
With wdDoc
  ActiveWindow.Visible = False
  'do processing here
  ActiveWindow.Visible = True
End With
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 01-05-2017, 09:41 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,165
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

Dropbox gets a bit sniffy if there are many third party downloads, so I have moved the progress bar to my web site - http://www.gmayor.com/Zips/ProgressBar.zip. You can adapt the code for 32 or 64 bit use with
Code:
Option Explicit
#If Win64 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
INCLUDEPICTURE with Mail Merge - showing same image on every document ccordner Word 0 10-23-2013 06:42 AM
phantom bitmaps showing on print but not in the document sour Word 1 04-08-2013 04:14 AM
How to create a progress bar AfterLife6 Excel 1 07-31-2012 08:43 PM
Object set to invisible yet I can still move its (invisible) animation paths?? seanspotatobusiness PowerPoint 0 05-23-2011 03:39 AM
Document showing up with all pictures missing? jplot Word 0 12-02-2009 06:50 AM


All times are GMT -7. The time now is 10:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft