Spell checking

G

Greg Maxey

Beth,

I explored that line, but in the document Larry (that is his name) sent to
me, I could change oScratchpad.Close to ActiveDocument.Close and the error
still occurred.
 
B

Beth Melton

I actually tested that scenario after reading a little more on how the
document was set up. If you create a document, add some form fields, protect
the document, show a UserForm and run your macro from a command button it
doesn't fail - it has to be something about the OP's code and/or the
document.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
T

Tony Jollans

If the scratchpad is closed, somewhere there is code that is causing it and
it should be identifiable by stepping through the code. However I have to
say that I would expect a different error if the document was already closed
unless, perhaps, there is some other variable holding a reference to the
document which might mean it is in some kind of 'close pending' state.

All guesswork, I'm afraid, but if it happens for you, Greg, then it is at
least it is document-related rather than environmental. Can you send me a
copy?
 
T

Tony Jollans

Greg has sent me the document - thank you. I know the cause - and can
recreate it - but not yet the best solution.

To demonstrate it very simply:

Create a new template.
Add two text formfields.
Set the exit macro from one of them to this:
Sub ExitTest()
Documents.Add
ActiveDocument.Close
End Sub
Save and close it.
Open a new document based on it.
Exit the formfield with the exit macro.

It seems you need to fully finish exiting the formfield before you can close
the document. I'm not sure of the best solution at the moment and am trying
a few things.
 
T

Tony Jollans

Hi Beth,

I just used 2007 - as it failed on that I didn't see a need to test on 2003
as well (the OP said he was using 2003).

Can you not recreate it with my demo either? If not then there is another
factor lurking somewhere.
 
B

Beth Melton

Hi Tony,

Okay, I can repro the error with the code you've provided but I'm still
can't seem to get the error to occur in either the original template or
Greg's cleaned up version Word 2007. Note Word 2007 is the only version of
Word I have installed and it's a clean installation so that could be a
factor. But, now with the ability to reproduce some type of error involving
closing a document from a protected document I may have a solution - it
works for the code and scenario Tony provided anyway:

Public Sub ExitTest()
Documents.Add
Application.OnTime When:=Now, Name:="CloseActiveDoc"
End Sub

Private Sub CloseActiveDoc()
ActiveDocument.Close
End Sub


Perhaps instead of using oScratchPad.Close the OnTime method could be used
instead?

Oh, and Larry if you're still following this, Mediam should be spelled
Medium. ;-)

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
G

Greg Maxey

Oh, and Larry if you're still following this, Mediam should be spelled
Medium. ;-)

LOL.

Didn't I point that out in the code? There were a few others that I spared
broadcasting. That from a notorious por speller myself ;-)

You and Tony have risen to the challenge and seemed to have provided the
cause and a workaroung solution. However, I think in this case Mssr.
Fuzzhead now agrees that the methods he employed where a bit over the top.

Thanks.
 
B

Beth Melton

Greg Maxey said:
LOL.

Didn't I point that out in the code? There were a few others that I
spared broadcasting. That from a notorious por speller myself ;-)

Oh yeah, you did - your method of commuicating this was MUCH better than
mine. ;-)
You and Tony have risen to the challenge and seemed to have provided the
cause and a workaroung solution. However, I think in this case Mssr.
Fuzzhead now agrees that the methods he employed where a bit over the top.

:)

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
T

Tony Jollans

Hi All,

Please accept my apologies for a rather lengthy post,

I'm not a lover of OnTime and, in this case, it doesn't seem workable.

Firstly, I can only make the OnTimed macro run at all if it is in Normal (I
haven't tried a different global template). Where did you put the code,
Beth? The OnTime instruction just seems to have no effect if the macro it is
trying to fire is in the document with the FormFields. Secondly, when I get
it to run by placing it in Normal, the FormField is not exited.

Both with this and the original problem, there seems to be a conflict
between FormFields and certain other actions; I really wish I understood
these things but can find no information at all.

My best guess is that when exiting a formfield, the action of entering the
next is somehow queued up waiting to run and that stops some other things
working - other things which also, perhaps, queue up in some way. When (by
accident or design) one of the queued actions is actioned or otherwise
released, the various other actions are then free to run (if they have been
remembered). There does seem to be a catch however ...

It seems that multiple DoEvents (and it does need more than one - whether
it's just a timing issue or whether there are in fact events which queue
further events or some other odd explanation, I don't know) will eventually
release whatever lock there is and the document can be persuaded to close
but when it does close it appears that it will return a "command failed"
error if it has returned one before.

In my test document ...

1. This fails

Sub ExitTest()
Documents.Add
DoEvents
ActiveDocument.Close
End Sub

2. This appears to work every time

Sub ExitTest()
Documents.Add
DoEvents
DoEvents
ActiveDocument.Close
End Sub

3. This closes the document but continues to loop and tries to close the
document with the formfields as well - reason being that the successful
close returns 'command failed'

Sub ExitTest()
Documents.Add
On Error Resume Next
Do
DoEvents
ActiveDocument.Close
Loop Until Err.Number = 0
End Sub

4. This gets round the above error and stops when the error is something
else (424 Object required)

Sub ExitTest()
Set newdoc = Documents.Add
On Error Resume Next
Do
DoEvents
newdoc.Close
Loop Until Err.Number <> 4198 ' Command failed
End Sub

5. This also gets round the error using a slightly different technique

Sub ExitTest()
Documents.Add
newdoc_name = ActiveDocument.Name
On Error Resume Next
Do
DoEvents
ActiveDocument.Close
Loop While newdoc_name = ActiveDocument.Name
End Sub

In Fuzzhead's document that Greg sent me, all the above fail and it appears
that the UserForm is an extra complication. Wrapping the code with Me.Hide
and Me.Show makes it work but introduces other problems to do with the order
of events and the display of the message box.

It does seem that hiding the userform is necessary so the easiest course of
action is to delay closing the document until the form is hidden later; this
requires the following changes (which I have tested in form frmWC but will
also be needed in frmCorc and anywhere else the code is duplicated).

1. Make oScratchPad a module level variable. Add at the beginning of the
module (after Option Explicit):

Private oScratchPad as Word.Document

and remove the definition from the spellcheck code in Test3_Click.

2. Remove the close of the scratchpad from Test3_Click. Although not
strictly necessary, I have hidden the document in my testing - to do this,
add this line where the Close was:

oScratchPad.Windows(1).Visible = False

3. It is possible that a user may click on "Spell Check" twice. If they do
that now there will already be a scratchpad document the second time and
opening another will leave the first one orphaned. To avoid this and just
unhide the previous document, change the open from:

Set oScratchPad = Documents.Add

to

If oScratchPad Is Nothing Then
Set oScratchPad = Documents.Add
Else
oScratchPad.Windows(1).Visible = True
End If

4. Lastly, after the form is hidden - that is in two places, CmdOK_Click and
CmdCancel_Click, add code to close the scratchpad if it is open, as per one
of the solutions above - I chose this one:

Me.Hide ' this line is there already
If Not oScratchPad Is Nothing Then
On Error Resume Next
Do
DoEvents
oScratchPad.Close wdDoNotSaveChanges
Loop Until Err.Number <> 4198
On Error GoTo 0
End If

This appears to work in my brief test but does need proper checking. It
would also be wise to add some error checking in case there is some other
error on closing - although unlikely a different error could cause an
infinite loop.

Hope this helps - and if anybody has a proper explanation which is better
than my work of fiction I would love to know what is really going on.
 
F

Fuzzhead

Greg, Beth & Tony,

I want to thank all of you for all your time that you have spent helping me.
I made the changes in my template that Tony described and did a test run. It
corrected the spelling mistakes and saved the corrections to my document with
on errors. After reading your input, I see that I way over did this template.
What I think I really need is a single text form that would come up when I
click on a comment or detail field in my document that I could enter the
information, run spell check if needed, then save it and go on to the next
field in the document. Can I use the same form for all the comment and detail
fields? I have sent Greg my original template with out all the forms and
could send it on to both of you Tony and Beth if that would help.
 
G

Greg Maxey

Larry,

I think you may be giving up to quickly on the Userforms. I have never had
a problem with them just your practice of using both a Userform and
Formfields at the same time.

I am working on some proposed changes for you (a little busy today) but may
get something to you to consider by tonight or tomorrow.
 
T

Tony Jollans

I haven't studied your template in any great detail - I was just looking at
the particular error - but what purpose are the userforms actually serving?
It seems to me you have FormFields in which data can be entered and you have
then superimposed userforms onto that to provide another method of input -
if so, why?
 

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