Home > Articles > Home & Office Computing > Microsoft Applications

This chapter is from the book

Closing a Document Without Saving

It's occasionally convenient to close a document without saving your changes. For example, you might create a new document to use as a scratch pad or to test out a new feature. Similarly, you might open an existing document, make some temporary changes, and then close the document without saving those changes. In each case, when you choose Office, Close, Word asks if you want to save the document. This not only slows you down by requiring an extra step that you don't need, but there's also the real danger that you might click Yes by accident.

To work around these problems, I use a macro that automatically closes a document without saving any changes and without prompting to save changes. Listing 3.2 shows the macro.

Listing 3.2. A Macro That Closes a Document Without Saving Changes

Sub CloseWithoutSaving()
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub

As you can see, this is a simple procedure that runs only the ActiveDocument object's Close method with the SaveChanges argument set to the constant value wdDoNotSaveChanges.

A more involved example is shown in Listing 3.3.

Listing 3.3. A Macro That Closes a Document Without Saving Changes and Then Reopens the Document

Sub CloseAndReopen()
    Dim currDoc As String, nState As Integer
    Dim nLeft As Integer, nTop As Integer
    Dim nWidth As Integer, nHeight As Integer
    ' Preserve the window position and dimensions
    With ActiveWindow
        nState = .WindowState
        nLeft = .Left
        nTop = .Top
        nWidth = .Width
        nHeight = .Height
    End With
    ' Close it without saving changes
    With ActiveDocument
        currDoc = .FullName
        .Close SaveChanges:=wdDoNotSaveChanges
    End With
    ' Reopen the document
    Documents.Open currDoc
    ' Restore the window
    With ActiveWindow
        .WindowState = nState
        If Not .WindowState = wdWindowStateMaximize Then
            .Left = nLeft
            .Top = nTop
            .Width = nWidth
            .Height = nHeight
        End If
    End With
End Sub

This macro first uses various properties of the ActiveWindow object to save the document's current window state, position, and dimensions. Then the code saves the document's full path name (as given by the FullName property), and the document is closed without saving changes. The Open method reopens the document, and then the window is restored to its former state, position, and dimensions.

  • + Share This
  • 🔖 Save To Your Account