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.
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.