Consulting

Results 1 to 3 of 3

Thread: Word: File and Close VBA time saver

  1. #1

    Word: File and Close VBA time saver

    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
    Last edited by Aussiebear; 11-12-2021 at 06:49 AM. Reason: added code tags to supplied code

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

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

Posting Permissions

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