PDA

View Full Version : Smart Close



marco75
10-22-2008, 09:53 PM
I want to write a VBA macro for Word that eliminates the ?Save changes to this document?? and ?Empty clipboard with large amount of data?? dialogs.

To do this, the macro will have to do the following actions, in that order:


Save all open Documents without prompt
Empty the clipboard
Close current document3 can potentially be eliminated, since there is a method of executing code whenever a document is closed.

The steps below where applied to the Normal Project in the VBE.

1. Save all open Documents

Insert as module:

Option Explicit
Public Sub SaveAndCloseAll()
With Application
.ScreenUpdating = False
'Loop through open documents
Do Until .Documents.Count = 0
'Save and close
.Documents.Save NoPrompt:=True
.Documents.Close
Loop
End With
End Sub

2. Empty the clipboard


Insert-UserForm (insert -> UserForm) this is to make sure you can use the DataObject Object.
Insert-Module. (Insert -> module)
Paste the following code:Option Explicit
Public Sub ClearClipBoard()
Dim oData As New DataObject 'object to use the clipboard
oData.SetText Text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
End Sub


Close VBE (Alt + Q or press the X in the top right hand corner).
Save the file.3. Do these things on close

Enter VBE
Add a new Class module, rename it to oAppClass
Paste the following code into oAppClass:

Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document)
SaveAndCloseAll
ClearClipBoard
End Sub

Add a new Normal module:

Option Explicit
Dim oAppClass As New oAppClass
Public Sub AutoOpen()
Set oAppClass.oApp = Word.Application
End Sub

=== Errors ===

Since I added these Modules to normal.dot, whenever I open a Word document, VBE pops up with the following message:

"Compile error: Procedure declaration does not match description of event or procedure having the same name"

The following line in oAppClass is highlighted:

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document)


Thanks for reading. Any help would be appreciated.

The WYSIWYG editor on this forum is quite good! Wish the text field was bigger, though.

marco75
10-22-2008, 11:01 PM
I just realized that "Save all open documents" is the wrong behaviour. Rather, when you close the Windows of a document (red X box), it should quietly save and close that document ONLY, e.g.


Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
SaveAndCloseDocument
ClearClipBoard
Cancel = False
End Sub



Option Explicit
Public Sub SaveAndCloseDocument()
ActiveDocument.Save NoPrompt:=True
ActiveDocument.Close
End Sub


=== Compile Error:

ActiveDocument.Save NoPrompt:=True

Wrong number of arguments or invalid property assignment
===============

What are the correct commands/syntax to save and close the currently focused document? :think:

CreganTur
10-23-2008, 09:17 AM
After you use ActiveDocument.Save you shouldn't see a save prompt- unless your code after that step makes the document dirty again.

Also, to close the current document you use:
ActiveDocument.Close

fumei
10-23-2008, 10:22 AM
You can do both with:


ActiveDocument.Close _
SaveChanges:=wdSaveChanges

This closes the active document with any changes. No prompt.