VBA Word 2003 File Close

  • Thread starter David Schnelbaker
  • Start date
D

David Schnelbaker

Word 2003,

In a Word 2003 template document I have a macro triggered by a CheckBox
event template that opens up another document, grabs data in that second
document and then is supposed to close that second document.
When I run the macro from the VBA editor it works exactly as designed with
no errors. But when the Macro is triggered as a result of the CheckBox
status change, I get a run-time error 4198 "Command Failed" at the line that
closes the document.

The offending line is just "ActiveDocument.Close (False)". I have put a
MsgBox with the describing the ActiveDocument so I know it is the right doc
that I am trying to close.

Any ideas?
 
D

Doug Robbins - Word MVP

It would be better to declare a variable as a Document and set that variable
to the document that you are opening rather than rely on the active
document. So assuming you declared Source as that variable with

Dim Source as Document

and then when you opened the document you used

Set Source = Documents.Open("FileName")

the when finished with that document, use

Source.Close wdDoNotSaveChanges

--
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, originally posted via msnews.microsoft.com
 
D

David Schnelbaker

Thanks for the reply Doug. Unfortunately using a Document variable gives
the same result. In my case I used PlaceholderDoc as the variable document.
But the Commance failed error still occurs on the statement Placeholder.Close
wdDoNotSaveChanges. Set up a checkbox that calls the Macro and you will see
what I mean.
It is very weird because the Macro runs fine if I just use F5.
 
D

Doug Robbins - Word MVP

Earlier to day I created the following code to help someone in the
mailmerge.fields newsgroup overcome a problem with documents that he was
inserting into another document were losing their margin settings. You will
not that it uses Source.Close wdDoNotSaveChanges and that does not cause an
error.

Dim Target As Document, Source As Document
Dim trange As Range, srange As Range
Set Target = ActiveDocument
Set Source = Documents.Open("C:\Users\Doug\Scratch\preface.doc")
With Source
Set srange = .Range
srange.Collapse wdCollapseEnd
srange.InsertBreak wdSectionBreakNextPage
End With
With Target
Set trange = .Range
trange.Collapse wdCollapseEnd
trange.FormattedText = Source.Range.FormattedText
End With
Source.Close wdDoNotSaveChanges

Show us your complete code and we may be able to spot why it is causing an
error in your case.


--
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, originally posted via msnews.microsoft.com
 
D

David Schnelbaker

Thanks Doug, I'll try working with the code suggestions in your example.
I'm relatively new to this VBA stuff so I appreciate your help.

Here is the part of the code string that I am trying to use. If you can see
where I am going wrong that would be great.

Private Sub DocOpenTest()

Dim aRange As Range
Dim sTrue As String
Dim sFalse As String
Dim oFld As FormFields
Dim sPath As String
Dim strText As String
Dim bProtected As Boolean
Dim cDoc As Document
Dim TrueFalseFileContents As Range

Set cDoc = ActiveDocument
Set oFld = cDoc.FormFields

'This saves the path variable as the default path in this case it's the same
as the active document regardless of where it is.
sPath = cDoc.Path

' Replaces single \ with a double \
sPath = Replace(sPath, "\", "\\")

'Set the Checked and Unchecked documents to be the same path as the active
document this could be in a sub-folder as well,
'just add it to the path ie sTrue = """" & sPath & "\\include\\Checked.doc"""
sTrue = """" & sPath & "\\RapidBuild Workshop Assumptions.doc"""
sFalse = """" & sPath & "\\NotApplicable.doc"""

'Sets the range to work with to be the one indicated by the "Bookmark1"
bookmark
Set aRange = cDoc.Bookmarks("Bookmark1").Range

'Turn off document protection

If cDoc.ProtectionType <> wdNoProtection Then
bProtected = True
cDoc.Unprotect
End If

'Depending on the Check1 setting the proper document is opened as a word
document
'The sTrue or sFalse document will be visible

If oFld("Check1").CheckBox.Value = True Then

Application.Documents.Open (sTrue)
Set TrueFalseFileContents = ActiveDocument.Content.FormattedText

'This reads the text contents of the opened doucment into the strText
variable.
strText = TrueFalseFileContents

'This closes the Active Document which is either sTrue or sFalse
ActiveDocument.Close wdDoNotSaveChanges

Else 'same routine as above except for the sFalse document
Application.Documents.Open (sFalse)
MsgBox sFalse
MsgBox ActiveDocument
Set TrueFalseFileContents = ActiveDocument.Content.FormattedText
MsgBox TrueFalseFileContents
strText = TrueFalseFileContents
ActiveDocument.Close (False)

End If

'This replaces the text in the "Bookmark1" bookmark with the text from the
opened STrue or sFalse document

aRange.Text = strText

'Rebuilds the bookmark so subsequent runs of the macro work.
cDoc.Bookmarks.Add Name:="Bookmark1", Range:=aRange


'ReProtect Document
If bProtected = True Then
cDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
 
D

David Schnelbaker

Hi Doug,

Based on the code suggestions you provided below, I rebuilt my macro. The
code is cleaner but unfortunately yields the same problem when it is called
as a result of the checkbox event. I've listed the new code below.

I am stumped by this one? Could this be a bug in VBA or in Word? Should I
report it to Microsoft? If I can't get this working I will need to change my
design to something else or abandon the idea altogether. I've already spent
considerably more time on this that was allotted for this project. Any
thoughts would be most welcome.

Sub May04Code()
'
' May04Code Macro
' Macro created 5/4/2009 by David Laing
'
Dim aRange As Range
Dim sTrue As String
Dim sFalse As String
Dim oFld As FormFields
Dim sPath As String
Dim strText As String
Dim bProtected As Boolean
Dim cDoc As Document, TrueDoc As Document, FalseDoc As Document
Dim TrueFalseFileContents As Range

Set cDoc = ActiveDocument
Set oFld = cDoc.FormFields

'This saves the path variable as the default path in this case it's the
same as the active document regardless of where it is.
sPath = cDoc.Path
' Replaces single \ with a double \
sPath = Replace(sPath, "\", "\\")

'Set the Checked and Unchecked documents to be the same path as the
active document this could be in a sub-folder as well,
'just add it to the path ie sTrue = """" & sPath &
"\\include\\Checked.doc"""
sTrue = """" & sPath & "\\RapidBuild Workshop Assumptions.doc"""
Set TrueDoc = Documents.Open(sTrue)
sFalse = """" & sPath & "\\NotApplicable.doc"""
Set FalseDoc = Documents.Open(sFalse)

'Sets the range to work with to be the one indicated by the "Bookmark1"
bookmark
Set aRange = cDoc.Bookmarks("Bookmark1").Range

'Turn off document protection
If cDoc.ProtectionType <> wdNoProtection Then
bProtected = True
cDoc.Unprotect
End If

'Depending on the Check1 setting the proper document is opened as a word
document
'The sTrue or sFalse document will be visible
If oFld("Check1").CheckBox.Value = True Then
Set TrueFalseFileContents = TrueDoc.Content.FormattedText
'This reads the text contents of the opened doucment into the strText
variable.
Else 'same routine as above except for the sFalse document
Set TrueFalseFileContents = FalseDoc.Content.FormattedText
End If
'This replaces the text in the "Bookmark1" bookmark with the text from
the opened STrue or sFalse document

aRange.Text = TrueFalseFileContents

'Rebuilds the bookmark so subsequent runs of the macro work.
cDoc.Bookmarks.Add Name:="Bookmark1", Range:=aRange

'ReProtect Document
If bProtected = True Then
cDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
CloseDocuments
End Sub
 
D

Doug Robbins - Word MVP

CloseDocuments is not a valid VBA command.

--
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, originally posted via msnews.microsoft.com
 
D

David Schnelbaker

Good point but still get the same results even if the correct VBA command is
included (TrueDoc.Close wdDoNotSaveChanges).
 
D

Doug Robbins - Word MVP

I do not know what is going on.

Do you get the error if you run a macro containing just (plus the necessary
assignment to sPath

sTrue = """" & sPath & "\\RapidBuild Workshop Assumptions.doc"""
Set TrueDoc = Documents.Open(sTrue)
TrueDoc.Close wdDoNotSaveChanges

I do not however understand why you have all of the quote marks in

sTrue = """" & sPath & "\\RapidBuild Workshop Assumptions.doc"""

or the \\

--
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, originally posted via msnews.microsoft.com
 
D

David Schnelbaker

Yes, the same command failed error occurs. I've even tried this by having
the macro call a function to close the file. Still doesn't work. I'm now
going to try this by replacing the form checkbox with an activeX control
checkbox and see what happens.

Thanks
 

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