Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Virginia, USA, Virginia, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Close and SAVE Word file in VB (Word 2000)

    I am trying to close and SAVE Word file in VB but I keep getting the "do you want to save" dialog box. I want my code to run a macro on each Word file in a folder, save the file with changes, close the file and go to the next one until all files in the folder are done. I do not want the interactive dialog box... I want this to run without intervention.

    Help file says to use: ActiveDocument.ActiveWindow.Close SaveChanges:=wdSaveChanges. THIS DOES NOT WORK... closes but does not save document at all!!!

    HELP!

    TIA,
    gg

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Close and SAVE Word file in VB (Word 2000)

    Try removing ActiveWindow:

    ActiveDocument.Close SaveChanges:=wdSaveChanges

    If you still have problems, something very strange is going on. In that case, could you post the complete procedure?

  3. #3
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Virginia, USA, Virginia, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close and SAVE Word file in VB (Word 2000)

    Removing Active window did not work. Here is the code.
    Public Sub ListDocNames()
    Dim MyArray() As String, WordObject As Object, strPath As String, N As Long
    Dim MyApp As Word.Application, strDir As String, strFile As String
    'Set MyApp = GetObject("", "Word.Application")
    'strDor = "C:Larry M. RosenCLOE"
    strFile = "*.doc"
    strDir = CurDir
    strDocName = Dir(strDir & "" & strFile)
    'MsgBox strDir
    N = 1
    ReDim MyArray(N)
    MyArray(1) = strDocName
    While strDocName <> ""
    Path = strDir & "" & strDocName
    Set WordObject = GetObject(Path)
    WordObject.Application.Visible = True
    'Insert RESUME STYLE CLEANUP macro
    RESUME_STYLE_CLEANUP
    WordObject.Save
    'WordObject.Close SaveChanges:=wdSaveChanges
    ActiveDocument.ActiveWindow.Close SaveChanges:=wdSaveChanges
    strDocName = Dir()
    N = N + 1
    ReDim MyArray(N)
    MyArray(N) = strDocName
    Wend
    'MyApp.Quit
    'Set MyApp = Nothing
    End Sub

    Thanks,
    gg

  4. #4
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Virginia, USA, Virginia, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close and SAVE Word file in VB (Word 2000)

    Using the object variable WordObject.Save made it work, but I still want to know WHY the Active document command does not work.

    Thanks in advance,
    gg

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Close and SAVE Word file in VB (Word 2000)

    Although you did mention VB in the subject of your post, I mistakenly assumed that you meant VBA. Strictly speaking, ActiveDocument has no meaning by itself within VB, it must be prefixed by a Word application object. But VB seems to understand that it must refer to Word; I have no idea why it would close the document but not save it...

    Some remarks:

    You're trying to fill an array MyArray, but since you use ReDim instead of ReDim Preserve, it is erased each time. Moreover, you do nothing with it, so it vanishes into thin air at the end of the procedure without ever having been used.

    It looks like you don't have Option Explicit in your modules. I strongly recommend that you set the option to force declaration of variables. It is easy to write code with unexpected results if you don't.

    There is no error handling; this is very dangerous if you're using Automation.

    Here is how I would write the procedure:

    Public Sub ListDocNames()
    Dim MyArray() As String
    Dim MyApp As Word.Application
    Dim MyDoc As Word.Document
    Dim N As Long
    Dim strDir As String
    Dim strFile As String
    Dim strDocName As String
    Dim strPath As String

    On Error Resume Next

    Set MyApp = CreateObject("Word.Application")
    If MyApp Is Nothing Then
    MsgBox "Failed to start Word"
    Exit Sub
    End If

    On Error GoTo ErrHandler

    ' Do you really need this?
    MyApp.Visible = True

    strFile = "*.doc"
    strDir = CurDir
    strDocName = Dir(strDir & "" & strFile)

    Do While strDocName <> ""
    N = N + 1
    ReDim Preserve MyArray(N)
    MyArray(N - 1) = strDocName
    strPath = strDir & "" & strDocName
    Set MyDoc = MyApp.Documents.Open(strPath)
    'Insert RESUME STYLE CLEANUP macro
    RESUME_STYLE_CLEANUP
    MyDoc.Close SaveChanges:=wdDoNotSaveChanges
    Set MyDoc = Nothing
    ' Get next doc name
    strDocName = Dir()
    Loop
    ' *** For testing only
    Dim i As Integer
    For i = 0 To N - 1
    Debug.Print MyArray(i)
    Next i
    ' *** End of For Testing only

    ExitHandler:
    'On Error Resume Next
    If Not MyDoc Is Nothing Then
    MyDoc.Close SaveChanges:=wdDoNotSaveChanges
    Set MyDoc = Nothing
    End If
    If Not MyApp Is Nothing Then
    MyApp.Quit SaveChanges:=wdDoNotSaveChanges
    Set MyApp = Nothing
    End If
    Exit Sub

    ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
    End Sub

Posting Permissions

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