Pause and Resume

B

BillCPA

I have a document - Doc A - that runs a macro. The Doc A macro opens other
Word documents - Doc B, C, D, etc. - and does some processing on them.

Once this automatic processing is finished on Doc B, I would like to display
a 'Continue' button, stop the macro (without displaying the VB code window),
make some manual changes to Doc B, click the 'Continue' button, and resume
processing in the macro where it stopped and do the same for Doc C, Doc D,
etc.

Is there any way to do this?
 
D

Doug Robbins - Word MVP

You could probably have the macro call a modeless userform at the completion
of its processing of Doc B, with that form containing the Continue button.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg Maxey

Doug,

Could you provide a few more details? I have spent the last hour trying to
incorporate a modeless userform some of your old code for processing a batch
of files. I can't get a modeless UserForm to stop the processing ;-(
 
D

Doug Robbins - Word MVP

I'll have a go. It was just an idea; not something that I had actually
done.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Hi Greg,

I had to take a slightly different approach using the following code in the
userform with the initialize event of the form creating an array of the
filenames in the folder and opening the first file and doing the processing
that is required. Then the user can do something else with the document
before clicking on the cmdNextFile button, which saves the file and closes
it and then opens the next file, does the processing, etc.

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Files As Variant 'An Array of the filenames of the files in the Folder
Dim FileList As String
Dim i As Long 'Counter for iterating through the Array of Filenames
Dim j As Long 'Gets set to the total number of files

Private Sub cmdNextFile_Click()
With ActiveDocument
.Save
.Close
End With
i = i + 1
If i < j Then
Documents.Open PathToUse & Files(i)
Else
Me.Hide
End If
'Do some processing
End Sub

Private Sub UserForm_Initialize()
PathToUse = "C:\Test\"
'Close all open documents before beginning
Documents.Close SaveChanges:=wdPromptToSaveChanges
'Set the directory and type of file to batch process
myFile = Dir$(PathToUse & "*.doc")
'Build an Array of the names of the files in the folder
FileList = ""
Do While myFile <> ""
FileList = FileList & myFile & "#"
myFile = Dir$()
Loop
FileList = Left(FileList, Len(FileList) - 1)
Files = Split(FileList, "#")
j = UBound(Files) + 1
'Open the first file
i = 1
Documents.Open PathToUse & Files(i)
'Do some processing
End Sub

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

A bit of tweaking required to the Initialize statement

Private Sub UserForm_Initialize()
PathToUse = "C:\Test\"
'Close all open documents before beginning
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
'Set the directory and type of file to batch process
myFile = Dir$(PathToUse & "*.doc")
FileList = ""
Do While myFile <> ""
FileList = FileList & myFile & "#"
myFile = Dir$()
Loop
FileList = Left(FileList, Len(FileList) - 1)
Files = Split(FileList, "#")
j = UBound(Files) + 1
i = 0
MsgBox Files(i)
Documents.Open PathToUse & Files(i)
'Do some processing
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BillCPA

Actually, I did get this idea to work. One of my concerns was building up a
bunch of subroutine calls without the same number of returns.

Doc A displays a menu with a Option Button whose code runs Macro A as its
final statement. Macro A opens Doc B, does some processing, then leaves Doc
B as the active document and displays the modeless userform with a Continue
Button as the final statement in Macro A. I believe that this allows all
subroutine returns to be completed - both Macro A and the code behind the
Option Button on the Doc A menu. No code is active at this point other than
the modeless userform.

I can make changes to Doc B manually, then click the Continue Button, which
(in my case) calls Macro A1 in Doc A, which saves Doc B, then does a test to
see if there are any more files to process. If so, Macro A1 calls Macro A as
its final statement, and the process repeats.

Seems to work - thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top