closing a word document

N

natanz

I have some code that opens a word document and inserts some useful
information in bookmarks and formfields:


Sub OpenTemplate(DocType As String)

Dim WordApp As Word.Application
Dim worddoc As Word.document
Dim TemplateFile As String
Dim TemplateDir As String

On Error Resume Next
Set WordApp = GetObject("word.application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("word.application")
End If
TemplateDir = "C:\Documents and Settings\My Documents\forms\"
TemplateFile = TemplateDir & DocType & ".dot"
WordApp.Documents.Add Template:=TemplateFile
Set worddoc = WordApp.ActiveDocument

If DocType = "type A" Then
Call TypeAInfo(worddoc)
End If

WordApp.Visible = True

Set worddoc = Nothing
Set WordApp = Nothing
End Sub

Public Sub TypeAInfo(worddoc As Word.document)
worddoc.Unprotect
worddoc.Bookmarks("projname").Range.Text =
ActiveWorkbook.Sheets("project info").Range("a7")
worddoc.Bookmarks("projnum").Range.Text = "FILE: " &
ActiveWorkbook.Sheets("project info").Range("b7")
With worddoc.FormFields("dropdown1").DropDown.ListEntries
.Add ("september")
.Add ("marching orders")
.Add ("Mr. Humboldt")
End With

worddoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

all this works fine as it is supposed to, but now things get a little
weird.

in the template file that i am opening (type A) in this example, i have
a autonew sub that sets the default save directory for this file. and
then at the end i have a fileclose sub that is supposed to reset it
when the user is done. here:

Option Explicit
Dim UserDocSavePath As String

Public Sub AutoNew()
Dim SaveDir As String
UserDocSavePath = Options.DefaultFilePath(wdDocumentsPath)
SaveDir = "C:...(some useful path here)"
Options.DefaultFilePath(wdDocumentsPath) = SaveDir
End Sub


Public Sub FileClose()
ActiveDocument.Close
Options.DefaultFilePath(wdDocumentsPath) = UserDocSavePath
End Sub

the code in word works fine, if i open the document based on the
template using file/new etc. BUT...
when i open the document using the automation from excel something
weird happens. If the user tries to close the document, a file
userform comes up prompting the user to save. if s/he does fine,
everything proceeds as normal, but if the user does not want to save
the document (as i suspect will be the case, most often) i get a
"'runtime error 4198' command failed." It seems like it has something
to do with the connection from excel to word. I am using early binding
(as far as i understand that concept (tools/references/.... word 9.0
object library).

This problem seems like it might be beyond the scope of this forum to
resolve, but i hope someone will have the fortitude to try to help me
work through this. When i googled 'run time error 4198' i found one
link on the msdn that implied that this may be some known bug in ms
word,
http://support.microsoft.com/default.aspx?scid=kb;en-us;330300,
but the case there was not exactly the same as mine. well who knows?
Thanks for any and all input.
 
N

natanz

by the way, i would be all to happy to email the specific documents i
am working with, if that would help someone in solving my problem.
 
N

natanz

well i came up with a workaround that does what i want, but if anyone
has any better ideas please contribute them. I changed the fileclose
sub in the word document as follows:

Public Sub FileClose()
On error goto closeerror
ActiveDocument.Close
closeerror: ActiveDocument.Close wdDoNotSaveChanges
Options.DefaultFilePath(wdDocumentsPath) = UserDocSavePath
End Sub

I hope that i am not the first person on this forum to soliloquize.

thanks for listening.
 

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