Pausing Macro, Returning to Pause Point

J

John

I am working on automating a document compilation task.
The number of potential documents to be included in the
single document number in the hundreds, and eventually
each document will be automated as well before inclusion
in the final compilated document. It is unlikely a user
would be able to complete the process in a single
sitting, both because of the number of documents involved
but also because the user may not have all of the
information needed to complete the task and may have to
look something up.

On a userform, I would like to be able to give the user
the option to quit and return to the project later where
they left off. Basically, I have one userform where the
user selects the documents to be included in the project.
When the user selects "OK", the documents selected begin
compiling. When a document is reached that is automated,
a userform for that document appears, the user makes the
appropriate selections and that document is then added.
The process continues when the user closes the userform
until the last document selected has been added.

I can save the values of the userform so that if the user
quits he can return to the userform with the values he
selected saved. The problem is that if a user quits, he
quits in the middle of a macro that is running. Is it
possible to not only return the values entered on the
userform, but to also be able return to the point in the
macro where the macro quit running when the user quit?

Thanks for any help
John
 
W

Word Heretic

G'day "John" <[email protected]>,

Yes, you can manually allow for this with some ultra tricky
programming to implement a complex status. I usually do something like
this:

At Stop time:

Create a document variable called MyProcessStatus with a content of
comma delimited var values, the first is your sub-process id (eg
Edit3) and following that as many vars as you need. Advanced
programming fully qualifies each keyword for backwards compat eg
"Id:=Edit3,OutLoop:=4,Inloop:=3".


At Start time

Go through your main loop, skipping what you can based on the
contents. When you get to the bit that re-uses the vars, set your vars
on the first loop pass, and reset the flag. Let's take an example of
mild complexity. I won't bother with the code for GetId, or GetVars
it's trivial string handling.


'Main

Id=GetId

PrepOurTables 'Always need to do this, no test needed

'This stuff we want to 'catch up on'

If len(Id)=0 or Id="Edit1" then Edit1
If len(Id)=0 or Id="Edit2" then Edit2
If len(Id)=0 or Id="Edit3" then Edit3


Then, inside each Edit routine, we see something like this

Public Sub Edit3()
Dim Inloop as long
Dim Outloop as long
Dim Restarting as Boolean

Dim Tmp as String
Dim k as Long
Dim L as Long

Restarting=(Len(GetID)>0)
If Restarting then
GetVars "Inloop",Tmp
Inloop = val(Tmp)
GetVars "Outloop",Tmp
Outloop=val(Tmp)
KillMyProcessStatus 'Not included, deletes doc var
end if

For k = 1 to 10

'do some stuff regardless of restart
'Blah blah


'do some stuff dependant on restart

if Restarting then
k=Outloop
else
'do some stuff
end if

'do some stuff regardless of restart
'Blah Blah


'Inner loop!
'===========

for l = 1 to 33

'do some stuff regardless of restart
'Blah blah


'do some stuff dependant on restart

if Restarting then
l=Inloop
else
'do some stuff
end if

'do some stuff regardless of restart
'Blah Blah

next
next

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)


John reckoned:
 
H

Howard Kaikow

As Steve points out, this can be done, but it requires very complex
programming:

1. Break your code up into independent segments.
2. When the code stops, the entire environment known to the program must be
saved in variables or .ini files or the Registry or ..., along with an
indicator of where you wish to resume the code.
3. When resuming the code, there must be setup code that assures that all
variables have the correct values and, most importantly, all documents are
open that were previously open, and the Selection, Ranges, bookmarks, etc,,
in each document are what they were when the code previously stopped.
 
W

Word Heretic

G'day "Howard Kaikow" <[email protected]>,

The registry is the worst place to save this Howey. Processing
document status MUST be saved with the document! I am halfway through
some process with THIS document :)


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Howard Kaikow reckoned:
 
H

Howard Kaikow

The Registry, or .ini file, is necessary to save stuff to identify which
document(s) to process.
Such info cannot be saved within the doc, as the software has no idea what
docs to open/validate.

P.S. I've likely been called everything, but I don't believe I've ever seen
the spelling "Howey".
Do folkes down under commonly use "ey" instead of "ie" in such words.
 
W

Word Heretic

G'day Howie,

Yes, Howie would be more accurate, so long as its diminuative its ok
by us Aussies :) Most people write mine as Stevey, not Stevie, so
its sorta interchangeable I guess :)

As for id'ing the doc in question - you Open it.

Here's an example use case

I have a document called War which gets created for any country I am
having communication difficulties with.

War creates soldiers, ships and planes, then assigns orders, then
kills soldiers ships and planes.

On day 1, America refuses to pay my tech writing bill so I open up a
new War document, call it War - America, and start filling out the
fields. This is a slow process and at lunchtime I stop. When I get
back from lunch, I find out China has pirated all my tools, so I open
a new War document, call it War - China, and start filling out the
fields.

The next day i come in and continue my mission with China, and the day
after. Day 4 I have completed the first part of War - China and have
to wait for the Engagement process to complete so I can run some
statistical analysis on the combat results. Whilst I am waiting for
the engagement, I browse to and re-open War - America and get back on
with the job of reducing Seppo land to a comma delimited
parenthetical phrase.

Thus, each doc carries its status, rather than having a registry that
looks like

America 2000
America 2004
China 2002
China 2004
Germany 2001
Germany 2003

etc

I never finished the war with America in 2000, but I recommenced a new
one in 2004 which is also still ongoing.

Then, for 'duplicate named documents' you still have to generate a
unqiue id and tie to a document variable. Thus, if we are going to use
a doc var, let's just stay with that.

Besides, I like to think of documents as convenient data containers.
Why maintain an articificial link to another, less convenient (eg no
RTF) data container - the registry.

Finally, and probably the most potent argument of the lot, should I
email War- America to Mr McGhie to complete for me whilst I am
completing War-China, or even to myself at home to continue from
there, IT WILL WORK. The registry way it will have no idea.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Howard Kaikow reckoned:
 
H

Howard Kaikow

Word Heretic said:
G'day Howie,

Yes, Howie would be more accurate, so long as its diminuative its ok

Alas, I'm not at all diminutive, indeed, I'll be exercising in a few
minutes.
by us Aussies :) Most people write mine as Stevey, not Stevie, so
its sorta interchangeable I guess :)

I've never seen that up here, but then we use a different English up here.
Hmmm, how many different English languages are there?
Microsoft Word seems to have 18 variations in Tools | Set Language for Word
2003.

Of course, since I was born in Kings county NY and spent most of my youth in
Queens County NY, I guess I could say that I speak the King's and Queen's
English, n'est-ce pas?

As for id'ing the doc in question - you Open it.

The method described works only when the macro knows in advance which
documents are to be used.
Does not work if the user can choose arbitrary documents, which is quite
common, so the registry/ini file would need to be used to maintain a list of
the documents that need to be opened when the code is next run.
 
H

Howard Kaikow

Page 182 of Fowler's Modern English Usage, 2nd edition, ISBN 0-19-281389-7
discusses "-ey, -ie, -y, in pet names. (By pet name is meant a name used
affectionately ...)".
 
W

Word Heretic

G'day "Howard Kaikow" <[email protected]>,

Well there ya go! Observation is backed by authority. I can't keep
this up or I'll have to change my name - Heretics cant espouse dogma
after all HAHHAHAHAHA

Stevie the Wordy Heretickler



Howard Kaikow reckoned:
 
W

Word Heretic

G'day "Howard Kaikow said:
I guess I could say that I speak the King's and Queen's
English, n'est-ce pas?

Does not work if the user can choose arbitrary documents, which is quite
common, so the registry/ini file would need to be used to maintain a list of
the documents that need to be opened when the code is next run.

This would only apply if the macro has to reach between created
documents. Otherwise, the user 'selects' the file of interest using
the file system - not the macro. In my example, building War-America
has nothing to do with building War-China.

Additionally, if one was to do something like "Choose from a list of
[Widget] [Specifications] documents, I still wouldn't be using the
registry due to the data size requirement. If we use an ini / txt /
xml file it is far easier for the user to hand tweak where necc.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Howard Kaikow reckoned:
 

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