How do I create a macro that will run automatically each time a Word 365 document is opened? A Bing search does not seem to throw up any up-to-date answers. The Help within Word does not even recognise the word AutoOpen!
The format of an AutoOpen routine looks like this:
Sub AutoOpen()
MsgBox "This document is copyrighted."
End Sub
For more information on AutoOpen and other Auto macros see this Microsoft article.
https://learn.microsoft.com/en-us/office/vba/word/concepts/customizing-word/auto-macros
If what you want to do only effects a single document when it is opened, you can also use the following.
Private Sub Document_Open()
MsgBox "This document is copyrighted:"
End Sub
For more information on the Document.Open event see this Microsoft article. https://learn.microsoft.com/en-us/office/vba/api/word.document.open
Related
I'm trying to run a few macros processes once I open my word doc. Is there any way I can run all of it automatically once I open the document? Thanks.
If the macro should run when the document is opened, name the macro AutoOpen.
In order to run a macro when creating a new document from a template, name the macro AutoNew.
The information in this article might be useful.
when you create a sub with this name "Document_Open()" it will run whoever document opens.
Sub document_open()
MsgBox ("Hello")
End Sub
Under the project tab in the VBA editor I use:
Private Sub Document_open()
'insert your macros directly or call them In here
End Sub
The document will need to be saved as a macro enabled document (.docm) for this to work.
I have templates I'm trying to add to users' PCs and have these templates easily accessible in the ribbon. I've been mostly successful except that the name of each macro is far too long.
It was Project1.ThisOutlookSession.OpenTemplate1. I realized the last portion was the name of the sub for opening that particular template. Then I realized I could change "Project1" to anything, whereby shortening the string.
I was wondering how I could go about changing the ThisOutlookSession portion or even better, remove the "Project1.ThisOutlookSession" portion altogether, leaving us with only whatever we name the sub (which will be the template name). My code:
'https://www.slipstick.com/outlook/hyperlink-templates/
Dim template As String
Sub OpenTemplate1()
template = "...Microsoft\Templates\FollowUpOnOrgSurvey.oft"
MakeItem
End Sub
Sub OpenTemplate2()
template = "...Microsoft\Templates\How Are We Doing.oft"
MakeItem
End Sub
Sub OpenTemplate3()
template = "...Microsoft\Templates\Option1.oft"
MakeItem
End Sub
Sub OpenTemplate4()
template = "...Microsoft\Templates\Option2.oft"
MakeItem
End Sub
Private Sub MakeItem()
Set newItem = Application.CreateItemFromTemplate(template)
newItem.Display
Set newItem = Nothing
End Sub
ThisOutlookSession is a default module name where your VBA macro resides. If you remove that part from the macro name there is a change the VBA sub will not be found at all.
I'd recommend creating a COM add-in instead of VBA macros. They were designed especially for distributing your Office based solution on multiple machines. VBA macros are not designed for distributing on multiple machines. See Walkthrough: Creating Your First VSTO Add-In for Outlook to get started quickly.
Also you may find the To distribute Microsoft Outlook VBA code to other users article helpful.
I have created Macro Enables Templates which will execute the Document_New sub when it opens but to have something for all word documents I can seem to get it right.
I have included the following Script into the Normal Project
Microsoft Word Objects > ThisDocument
Private Sub Document_Open()
'When you open an Exisiting Word Document
MsgBox "This Worked"
Call SomeMacro
End Sub
Private Sub Document_New()
'When you open a New Word Document with Word already Open
MsgBox "This Worked"
Call SomeMacro
End Sub
Note: This will only auto run the Macro when you open a Word Document that exists already. It will not work if you open Word using the Application Button as found in the Start Menu > Programs >Microsoft Office.
I have seen people referring to Event handlers, but I am not sure what that it.
I am using Word 2010.
Want to see if there is a way for a Macro to execute if the Word.exe is run
For a macro that executes whenever Word starts up use the macro name: AutoExec. Be sure to place the macro in a "normal" module, not a class module or ThisDocument (which is also a kind of class module).
Just a quick question. Is there a way to create a front page as a dashboard with buttons in word to assign to the macros created?
I have to paste the code in every word sheet I open each time I want to run it.
I would like to open a word sheet of my choice and the modules to be exported into that sheet each time where I can run them by aid of buttons.
Try this:
Place your Code (Modules and Userforms) into the Normal Project, if not in there already. If they are not there already then drag each Module and Userform from your current Project to the Normal Project.
Then in the Normal Project select the following:
Microsoft Word Objects > ThisDocument
The Select Document as your Object and then New as your Procedure.
Then in the Document_New Sub Call your "DashBoard" or the Macro which initialises your code and bring up the first Userform.
Private Sub Document_Open()
'When you open an Existing Word Document
MsgBox "This Worked"
Call SomeMacro
End Sub
Private Sub Document_New()
'When you open a New Word Document with Word already Open
MsgBox "This Worked"
Call SomeMacro
End Sub
Note: This will only auto run the Macro when you open a Word Document that exists already. It will not work if you open Word using the Application Button as found in the Start Menu > Programs >Microsoft Office.
EDIT:
To run a macro if the Word Application has been Executed (Started from the Microsoft Word Application Button) then have a macro in the Normal project named AutoExec.
Sub AutoExec()
MsgBox "This Messages Appears Every time"
End Sub
I'm new to VBA. I'm trying to update the Table of Contents in my Word document everytime I open the document, but it does not seem to update it at all.
In ThisDocument I've done the following:
Private Sub Document_Open()
ActiveDocument.TablesOfContents(1).Update
End Sub
Can anyone help me?
The issue is probably the use of Document_Open - you have to set up the event in the template, not the document you are opening. You can also use an Auto_Open macro. Note that ActiveDocument may also be part of the culprit - the document you open may not yet be the actual active document when you kick the Document_Open event - you may need to set a reference to the document you're opening like:
Dim doc As Document
set doc = Documents.Open(your path here)
doc.TablesOfContents(1).Update
Finally, your Macro Security settings could be disallowing anything from executing.
In all cases, have a good read of Take Control of Microsoft Word Through Events and Running a macro automatically when a document is created, opened or closed.