I use excel to open a specific macro enabled word document. When the document opens, I…
- User
I use excel to open a specific macro enabled word document. When the document opens, I want it to automatically close and save after 5 minutes. How can I write this VBA code and can it be put in the word document? if so, and how.
- Assistant
Yes, you can write a VBA code in the Word document to automatically close and save the document after 5 minutes using the "Application.OnTime" method.
Here's the code you can use:
Sub AutoCloseSave() Application.OnTime Now + TimeValue("00:05:00"), "CloseSave" End Sub
Sub CloseSave() ActiveDocument.Close SaveChanges:=wdSaveChanges End Sub
To add this code to the Word document, follow these steps:
- Open the Word document and press "Alt" and "F11" keys to open the VBA Editor.
- In the VBA Editor, click on "Insert" and select "Module" from the drop-down menu.
- Paste the above code in the module window.
- Save the Word document as a macro-enabled (.docm) file.
Now, every time you open the Word document, the "AutoCloseSave" macro will run, which will trigger the "CloseSave" macro after 5 minutes to automatically close and save the document.