Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default closing a word document

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.ListEntri es
.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. he

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...en-us%3B330300,
but the case there was not exactly the same as mine. well who knows?
Thanks for any and all input.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default closing a word document

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default closing a word document

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating Document Without Closing and Re-Opening Randy Excel Discussion (Misc queries) 2 March 4th 09 05:01 PM
How can I convert an Excel document to a Word Document has3801 Setting up and Configuration of Excel 1 June 11th 08 11:16 PM
convert excel document to word document (not a picture) frendabrenda1 Excel Discussion (Misc queries) 2 October 6th 06 03:55 PM
How do I undo changes after saving and closing a document astrid Setting up and Configuration of Excel 2 April 7th 06 10:30 PM
Changing a Word Document to a CSV Formated Document Team1 New Users to Excel 3 February 6th 06 11:29 PM


All times are GMT +1. The time now is 09:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"