Document_Open does not work in Word 2007 template - vba

For employees to work, I need to put some functionality into the Document_Open macro. I want to put this macro in a template (.dot) and place it on the network. Office 2007 is installed on most machines, and I'm testing on it.
I create a document, insert the macro code, save it as a template (.dot). I use the following macro code as a test:
Sub Document_Open()
MsgBox "ABC"
End Sub
Next, I create a document (.doc) and change the document template on my created template. I allow the execution of macros, the macro work. I save and close the document, when I reopen the document and enable macros, our macro does not work. What the problem is I can’t understand. Thank!

Related

Word VBA AutoNew macro not firing when opening Word

Using Word 2013.
I found this previous question that is similar, but there was no accepted answer, and I'm not trying to build an add-in, so I hope I don't need something complex.
I have AutoNew and AutoOpen macros in my Normal.dotm. They are working great except that neither one of them fires when I open Word from the taskbar or other icon. In that situation, Word opens with the blank "Document1," but without the initialization I want.
Suppose my macros display a message with the document's name when a document opens or is created. I know this is silly, but it seems like a workable example.
If I open a document by clicking on it in File Explorer or by using File | Open or File | New, then the appropriate macro will fire and the message will pop up.
But if I just open Word from scratch, then no macro fires, and there is no message displaying "Document1."
I tried AutoExec to cover this situation, but when AutoExec fires, there is no active window and no document loaded yet.
Ideas?
The example cited in the question not "workable" because it makes no sense to want to show the name of an automatically generated document.
In any case, only AutoExec will fire when the Word application is started - this is by design. AutoNew does not fire when Word creates a new, default document when the application starts. (You'll also notice if you immediately open another document, without doing anything to the default document, that document is closed automatically. In a manner of speaking, this default document doesn't exist until the user takes action on it.)
What can work is to explicitly create a new document during AutoExec. This will have the effect of generating a document in place of the default document. The document will have the same name as that default document. The only different would be that this document will not close automatically when another document is opened.
When the document is created then AutoNew (and Document_New) will be triggered.
Sub AutoExec()
Documents.Add
End Sub
In Word 2016 64bits AutoOpen is triggered before AutoExec.
So if you Open a file, when AutoExec runs you can open a new file only if there is no file already opened.
As a result when you open a file you don't have another blank document opened as well.
In Word 2010 and 2013 AutoExec is triggered before AutoOpen.
So the solution cannot be applied.
But you can ask Word to close the blank file in the AutoOpen macro.
Have you tried just adding code for the Document_New() and Document_Open() events in your Normal.dot file? I just tried it with mine and however I opened the test document I got the Document_Open() to fire. The Document_New() worked as well.
Screen shot of my setup for testing. The relevant msgbox includes the name of the document.

Excel quick access toolbar - run macro from active instead of previous workbook

I have a custom button (not an add-in, just a "shortcut" button) in Excel's quick access toolbar to run a macro in the active workbook. The workbook is modified and saved daily with a new filename. When the link is first created in the active workbook (call it Version 1) the macro runs fine. However the following day (filename now Version 2) clicking on the macro button opens workbook Version 1 and runs the macro saved in Version 1.
Is there a simple way to break the link and run the macro in the most recent active workbook?
The code is basic - it just opens up a userform and is saved in "ThisWorkbook"
Sub OPEN_DATA_USERFORM()
ufDATA.Show
End Sub
I will try to guide you through this, I hope it helps.
What we need to do is we need to call OPEN_DATA_USERFORM() from our "PERSONAL.XLSB" file. In VB editor screen, in the project explorer you will find a file called "PERSONAL.XLSB", you need to add an "Module" to that and add following code:
Sub KickOff()
Call Application.Run(Excel.ActiveWorkbook.Name & "!OPEN_DATA_USERFORM")
End Sub
By this we will be able to call the userform.show function from our PERSONAL.XLSB which is always running in the background when you open the excel.
PS: I am assuming your OPEN_DATA_USERFORM() is coded in your daily workbook.
UPDATE: PERSONAL.XLSB
Your file should be visible in your project explorer like below:
If it is not there record a dummy macro and select your personal file like below then it should appear in your project explorer in VBA screen:

Word 2013: New File from template disables macros

I have a template with a macro that runs every time I open the .dotm file.
However when I go to New and click on the template it generates the new file based on the template and with references to its code but the macro doesn't run.
What I need for the macro to do is to open a form in the template.
Take a look at this article (old but still relevant) which should help:
Running a macro automatically when a document is created, opened or closed.
Basically, you need to handle the AutoNew() and possibly the AutoOpen() events in your code module.

Auto displaying form on opening a template file, dotm from explorer

I have written a form based document generation macro (in VBA) for distribution to a sales team.
For their ease of use, I want to provide a self-contained file which will display the form as soon as the document is opened.
Using AutoOpen I can get the form to display as intended if word is already open and the dotm file is opened within. However, if I double click the file from within explorer, nothing happens and I have to launch the macro manually. I thought AutoExec might allow this but no luck there. I've spent considerable time trying to get this to work through googling etc. but I'm not getting anywhere.
How can I make the form display even when the file is opened with a double click? Is it possible to do this without having to change normal.dotm for each user?
For further background, I am using Word 2013 with macros fully enabled during testing. The dotm file is stored in a trusted location.
I am using a macro to launch the form like this...
Public Sub AutoOpen()
StartPage.Show
End Sub
I have tried using AutoExec as well to no avail.
In the "generator.dotm" file got to Visual Basic and go in to the "ThisDocument" Microsoft Word Object.
At the top of the Visual Basic Editor select "Document" in the left hand side and then click on "New" on the right hand side. Private Sub Document_New() method will appear for you to be able to edit. Then you can call your userform in there. Similar to:
Private Sub Document_New()
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
End Sub
Save your Generator.dotm and double click it through Windows explorer and you should get the results that you would like.

Powerpoint VBA code to appear on all new documents

I have a macro in Powerpoint 2010 to open a new document based on a template based in a central location, to be installed on several machines.
Sub PowerpointTemplate()
Application.Presentations.Open ("file")
End Sub
Powerpoint does not save the macro. Even when setting "Macro in:" to "All open presentations", it seems to reset this selection to "Presentation 1". The macro works for the time that I have this current document open, but once I close Powerpoint and reopen it, the macro has been removed.
Is there anyway to set the macro to permanently apply to all presentations?
You need to create an Add-in and load it in order to make the code available to all open presentations.
Setting Macro In to All Open Presentations simply SHOWS you the available public macros in all open presentations; it has no effect on where the macro code is saved.