Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error 4198 - Command Failed 1

Status
Not open for further replies.

WalkerEvans

Technical User
Jan 17, 2007
171
US
Hi!

Can anyone shed some light on this? I am getting the following error:

Run-time error '4198'
Command Failed

This is showing up at the bottom of a macro that opens a Word document, allows changes to be made, prints and deletes that document and then opens a form-field document which is then printed and closed (in theory). The problem is that I keep getting the above error after everything but the close is done, leaving me with an open form-field document containing sensitive data.

What we have here should be simple; I've used this same line in many other macros and it has worked flawlessly. I'm at a loss to understand why it is failing here. The full code in this section is (problem line hi-lited):

Code:
Sub ender3()
For Each aField In ActiveDocument.FormFields
    Count = Count + 1
Next aField
For j = 1 To Count
    ActiveDocument.FormFields(j).Select
        With Selection.Find
        .Text = "0"
        .ClearFormatting
        .Replacement.Text = "Ø"
        .Replacement.ClearFormatting
        .Execute Replace:=wdReplaceAll, Forward:=True
    End With
Next j
ActiveDocument.FormFields(1).Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=("xxxx")
Count = 0
Call GetOut2
End Sub

Sub GetOut2()
'Prints form, reactivates Spell & Grammar
'Checks, then closes form without Save
ActiveDocument.PrintOut
ActiveDocument.ShowSpellingErrors = True
ActiveDocument.ShowGrammaticalErrors = True
[highlight]ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges[/highlight]
End Sub

Help! [hairpull]


----------------------------------------------------------------------------------
[small][ponder]"What was the psychiatric diagnosis of the man who jumped into the river in Paris?"[/small]
"He was in Seine!" [lol]
 
ActiveDocument.PrintOut[!], False[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH,

Nope, with or without that False appended, it still fails with the same error on that last line. Thanks for trying.

Any other ideas? I'm getting terribly frustrated by this, and by the feeling that the answer is going to be incredibly simple.

[hairpull3]

----------------------------------------------------------------------------------
[small][ponder]"What was the psychiatric diagnosis of the man who jumped into the river in Paris?"[/small]
"He was in Seine!" [lol]
 
OOps, sorry for the typo.
Get rid of the comma in my suggestion.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

PH,

Same result, with or without the comma. The only difference with the False inserted was the appearance of a message informing me that the document was being printed. It is still failing with the same error when it comes to the "Close" line.

Any different thoughts or suggestions, anyone?

[hairpull3]

----------------------------------------------------------------------------------
[small][ponder]"What was the psychiatric diagnosis of the man who jumped into the river in Paris?"[/small]
"He was in Seine!" [lol]
 
Perhaps this ?
Code:
ActiveDocument.PrintOut False
[!]'[/!]ActiveDocument.ShowSpellingErrors = True
[!]'[/!]ActiveDocument.ShowGrammaticalErrors = True
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

PH,

This didn't work either. The same "4198" error every time on that "Close" line.

As noted in my first post, the most frustrating thing is that this same code works perfectly in other documents; it even works in another spot earlier in this document! There has to be some simple, little thing that I'm not seeing. I've run all of the code and everything works, until it hits that last line.

I'm going to keep poking at this - maybe I can find the problem before going completely nuts.

[glasses]

----------------------------------------------------------------------------------
[small][ponder]"How many surrealists does it take to screw in a lightbulb?"[/small]
"Two -- one to hold the giraffe, the other to put the clocks in the bathtub." [lol]
 

PH,

I went through my code again, line by line, and still couldn't find anything wrong. I ran it once more thinking that a screen shot of the error message might help, but I couldn't get one - the @*#! thing ran perfectly! On retrying it continued to run perfectly a dozen times, even though not even a single character had been changed.

This is the third time this sort of thing has happened to me in the last 18 months. Perhaps it's paranoia, but it seems as if Bill Gates & Co. are actually trying to mess with us by designing random failures into VBA, failures that we spend hours trying to find only to have them disappear as suddenly as they came.

Anyway, thanks for trying to help me figure this out. I'm sorry that we both had to waste a lot of time and skull-sweat chasing this phantom around.

[banghead]

----------------------------------------------------------------------------------
[small][ponder]"How many surrealists does it take to screw in a lightbulb?"[/small]
"Two -- one to hold the giraffe, the other to put the clocks in the bathtub." [lol]
 
I believe it has something to do with:
Code:
ActiveDocument.ShowSpellingErrors = True
ActiveDocument.ShowGrammaticalErrors = True

Although you do not show any code, I am assuming that somewhere you have these set as FALSE.

These are in GetOut2, which is called after you have protected the document for forms.
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=("xxxx")
Count = 0
[highlight]Call GetOut2[/highlight]

The instructions themselves (setting ShowSpellingErrors, ShowGrammaticalErrors to True) will not fail, but the result of being True means Word will attempt to re-display the contents. To display any spelling errors. However, it can NOT re-display into protected sections, as that changes content.

May I ask why you use:
Code:
For Each aField In ActiveDocument.FormFields
    Count = Count + 1
Next aField
For j = 1 To Count
to loop through the formfields collection, just to get a Count?
Code:
For j = 1 To ActiveDocument.FormFields.Count

faq219-2884

Gerry
My paintings and sculpture
 

Gerry,

Thanks for the feedback! If my problem had anything to do with the spell and grammar checks it won't show up again; these were moved to where they should have been to start with.

This is code that I wrote a number of years ago when I was first learning VBA, so it is definitely not as smooth as it could be. However, for all those years it worked without a problem leaving me still puzzled as to why it failed now.

As to why the separate Count loop, at the time I didn't know any other way to get the count I needed for the replacement loop that follows. There is a lot less hair on my head now than when this was written, and hopefully a bit more inside; if this was written today it would be tighter and more compact. I hadn't fooled with it before for the best of reasons - it wasn't broken. As long as it worked and kept everyone happy there was no point in messing with it. Now that I have had a problem if time allows I'll go back and rewrite the whole thing. In the meantime the folks who rely on this to make a tedious job more bearable are happy again.

Here's a purple-pointy-thing for you!

[glasses]

----------------------------------------------------------------------------------
[small][ponder]"How many surrealists does it take to screw in a lightbulb?"[/small]
"Two -- one to hold the giraffe, the other to put the clocks in the bathtub." [lol]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top