PDA

View Full Version : Word: File and Close VBA time saver



james_martin
11-11-2021, 12:54 PM
Hi All,

I do lots of client Word files every hour,
and created a macro to close them after I have saved.
So you leave the Word App open and save about 5 seconds
when run via the above ribbon short cut. I changed the
Symbol to "X".

This works very well, if you have saved or unsaved files
open, and even if a protected view file is downloaded from
a web browser.

For a standard work day, if you use 52/53 times, I save
1% of my work hours, so 1% more efficient from just this
macro, which is not my main time saving macros in
Outlook and Word.

This one drove me a bit nuts to get it to work.
Is there a better way to code this? It works fine so far.
I have copied the macro into Excel and adjusted, so
is adjustable.



Sub WordFileClose()

'SOURCE: James Martin - May be simpler vba to do this but I could not work out other methods, this works.

Word.Application.screenupdating = False

Dim WrdPV As ProtectedViewWindow

If Word.Application.ProtectedViewWindows.Count >= 0 Then GoTo Normal

'this formula counts open and recent (viewable via right clicking on WordApp in Taskbar).
Protected: If Word.Application.Documents.Count >= 0 Then GoTo NoDoc

NoDoc: If Word.Application.ProtectedViewWindows.Count = 0 Then

Exit Sub

End If

Word.ActiveProtectedViewWindow.Edit

Word.ActiveDocument.Close

Exit Sub

Normal: On Error GoTo Protected

Word.ActiveDocument.Close

Exit Sub

Word.Application.screenupdating = False

Exit Sub

End Sub

gmayor
11-11-2021, 09:49 PM
If you are saving then closing, why not combine the operations? The following will prompt for documents that have never been saved, and close others saving any changes.

Sub SaveAndClose()
If Documents.Count > 0 Then
On Error GoTo err_Handler
ActiveDocument.Close wdSaveChanges
Else
MsgBox "No document open!", vbCritical
End If
lbl_Exit:
Exit Sub
err_Handler:
MsgBox "Document not saved", vbCritical
GoTo lbl_Exit
End Sub

james_martin
11-12-2021, 07:54 AM
Thanks, will look to do a version with save as well, on my macro.

Mine does nothing if the "X" macro ribbon short cut is pressed by mistake.

And will work if a client document is downloaded for viewing only and is

In Protected View.

Deliberately want no MsgBox to save time for repetitive tasks.
And so other staff have no bugs/stops either with MsgBox or VBA break.

But good to see how to do as I am not good at them