Share via

AutoExit macro needs help

Anonymous
2024-09-10T01:58:38+00:00

Hi, could someone help me with this macro?

Sub AutoExit()

Dim myAddIn As String

myAddIn = "C:\Users...\AppData\Roaming\Microsoft\Word\STARTUP\MyMacros.dotm"

Documents.Close myAddIn, True

MsgBox "MyMacros closed and saved."

End Sub

I can't get it to work.

The context here is that I keep most of my macros in a template file called MyMacros, located in Startup. Sometimes I open MyMacros to adjust the VBA a bit. I open MyMacros using a macro that sets the document to invisible, which helps me cut down on screen clutter. But because the file is invisible, I sometimes forget to close it and save it. As a failsafe, I wrote the AutoExit macro above, so that, if open, MyMacros would get closed and saved every time I close Word.

Any advice on making this work would be much appreciated.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Charles Kenyon 167.4K Reputation points Volunteer Moderator
    2024-09-10T02:40:46+00:00

    My personal advice would be to not hide MyMacros while you are working on it.

    Are you getting any error message?

    What is happening that tells you it does not work?

    Documents.Close would close all documents.

    How about using ThisDocument.Close?

    That closes the code container.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-09-11T00:26:03+00:00

    I've updated the AutoExit macro in hopes of making it a bit more sensitive. Here's what I've got:

    Sub AutoExit()

    Dim doc As Document

    Dim IsMM As Boolean

    IsMM = False

    For Each doc In Documents

    If doc.Name = "MyMacros.dotm" Then IsMM = True 
    
    doc.Close wdSaveChanges 
    
    If IsMM Then MsgBox "MyMacros closed and saved." 
    

    Next

    End Sub

    If I haven't opened MyMacros, nothing happens when I close Word.

    If I have opened MyMacros, and have left it open, Word closes and I don't see the msg box. Afterward, ~$ files show up for both Normal and MyMacros. When I reopen, I can tell that MyMacros is still open from the last session, because when I run my CloseMyMacros macro, I get a msg box at the end confirming that MM has been closed and saved. No further trouble thereafter.

    So the problem remains: the AutoExit macro isn't working, for reasons unknown (by me).

    Was this answer helpful?

    0 comments No comments
  3. Charles Kenyon 167.4K Reputation points Volunteer Moderator
    2024-09-10T05:30:29+00:00

    There's no error message, but I can tell it's not working because I end up with a ~$ temp file in the Startup folder after closing Word.

    I've tried "Documents.Close True" instead of my original code, and I'm having the same result.

    Is something amiss with how I'm using the AutoExit function?

    The temp folder may indicate a failure to save, but could be from something else.

    Do you get your message box?

    Try making a change and then closing Word. What happens?

    P.S. I do not use AutoExit. Someone else may be better to advise you.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-09-10T03:11:16+00:00

    There's no error message, but I can tell it's not working because I end up with a ~$ temp file in the Startup folder after closing Word.

    I've tried "Documents.Close True" instead of my original code, and I'm having the same result.

    Is something amiss with how I'm using the AutoExit function?

    Was this answer helpful?

    0 comments No comments