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).
Related
I have a macro that captures word events. In this macro in the Document_Open event, I'm going to show the dialog box and force word to quit. With the latest update, the exit instruction causes me a serious error that is shown to me when I go to recall the same document. If I delete the Word close statement and do it manually everything works fine.
I tried different solutions but could not solve the problem.
I use Word 365 .
This is the event class where i call the routine and then close the document and quit from Word.
Public WithEvents App As Application
Private Sub App_DocumentOpen(ByVal Doc As Document)
StampaModulo
Doc.Close False
App.Quit
End Sub
This is the module where in AutoExec I associate the application with the class.
Dim x As New EventClassHandler
Public Sub AutoExec()
Set x.App = Application
End Sub
When I open the same word document, the document is in the disativated element and I receive this message.
I tried also to force the quit in the routine of the print of the word document but I have the same problem.
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
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.
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