Run macros once Word doc is opened - vba

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.

Related

How to create an AutoOpen macro in a Word 365 document

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

Document_New Not Working

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).

Creating buttons or front page for VBA word

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

Add-in with predefined shortcut key?

Firstly, this is all in Excel 2007, FYI.
I'm trying to set up an Add-in that will use a shortcut key to run a simple macro. I want to define that shortcut key so that I can simply tell my users to press "ctl+alt+l" to run the macro, hopefully keeping explanations to a minimum on my team since the user interface would be unified.
My code to set up the shortcut key is right here:
Sub LiteralizeShortcutKey()
Application.OnKey "%^l", "Literalize"
End Sub
When I run the macro it works just fine.
I then create a Workbook macro to autoload when the excel file opened.
Private Sub Workbook_Open()
LiteralizeShortcutKey
End Sub
This works fine when I open the workbook. But it all falls apart when I export the workbook to a .xlam and then load it into another workbook. My little LiteralizeShortcutKey() Sub won't load, so I can't use my predefined shortcut key.
Thoughts?

Updating/creating Table of Contents in Word using VBA

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.