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.
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.