+ Reply to Thread
Results 1 to 3 of 3

closing a word document

  1. #1
    natanz
    Guest

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


  2. #2
    natanz
    Guest

    Re: 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. #3
    natanz
    Guest

    Re: 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1