I need to create a document from custom template using command line. This template contains the AutoOpen routine and it is executing if I open the template itself:
path_to_msoffice/winword.exe "c:/users/michael/Application Data/Microsoft/Templates/my_template.dotm"
But when I'm trying to create a document based on the template, it is created but the macro routine is not executing:
path_to_msoffice/winword.exe /t"c:/users/michael/Application Data/Microsoft/Templates/my_template.dotm"
I have Office 2010, macro execution is allowed.
How can I solve this problem?
Use the /z switch instead of the /t switch. That enables the NEW event. (for a complete list of command line switches see https://support.microsoft.com/en-us/help/210565/how-to-use-startup-command-line-switches-to-start-word-2010-word-2007)
Please note that AutoOpen is not what you want when creating a new document from a template. That is what fires when an existing document linked to the template is opened.
AutoNew would be the macro that fires when a new document is created using File/New, but the /z switch triggers the New event, not that macro. You find the event in the template's ThisDocument (class) module. Click on the list at the top left of the Code Window and choose Document. Then choose Newfrom the list at the top right to get the stub if Word doesn't create it automatically.
Related
I have a template (dotm) file in the word startup folder -C:\Users(username)\AppData\Roaming\Microsoft\Word\STARTUP.
I want to have a macro (vba code) in this dotm file that delete this template from the startup folder. The template is an add-in with custom RibbonXML. I want one of my buttons to be "uninstall" button, which deletes the template file from the startup folder.
What you envision is not possible.
In order for the macro in the template to run, that template must be open or loaded as an add-in. In that case, there's a "file lock" on the template: it cannot be moved or deleted.
Deleting the template must be done from code that is not associated with the template, when the template is closed and there is no longer a file-lock.
Edit based on new information edited into the question
Possibly, code in another file could do this, although it's not certain the file lock will be released in a timely manner after the Unload method removes it from Word. It might require running code on a Timer to repeat the Kill command until it's successful.
Use the idQ attribute for the RibbonX tab/group that will contain the "Uninstall" button. This lets other RibbonX share the tab/group by using the same idQ value. Put the Ribbon XML defining that button, as well as the code for it, in the other file. When both files' Ribbons are loaded it will appear to the user as one Ribbon.
The startup template would load this other file as an add-in using the method Application.AddIns.Add.
In our office we are currently using templates containing macro's. We are about to upgrade to Office 2016, but unfortunately the macro's don't work completely as they used to.
The current implentation is that a template is opened from a custom dialogue, and that a Document_New() is called in the template. This does not seem to work anymore: the Document_New() is only called when a template is opened from the file explorer, not when it's opened by a Documents.Add() in another macro.
Alternatively, I found a lot of solutions where Documents.Add is called, and then other functions are being invoked on that new document. For example
Set doc = Documents.Add(Template:=strSkeuze, NewTemplate:=True)
Call MsgBox(doc.Name)
In Word 2016 this doesn't seem to work. The MsgBox isn't invoked and when I step through the code in debugging mode, the code stops executing after the Documents.Add().
However I cannot find anywhere that this is a known change and I am looking for a workaround to still execute code, either from the template like with the Document_New() or from the parent Macro that opens the document.
Could someone tell me whether this is still possible and how to solve this?
You ought to be able to detect the added document using the Application's Document_New event, either that it fires (presuming that you have been using the Document's Document_New event) or by generating the event artificially by counting open documents on the first action taken after the document is added.
I have an existing Macro Enabled Template which I would like to add Content Control to, to be available on the screen when the Template is accessed.
I know how to add the Content to a Word Document and then save it as a Template. The problem is that I cannot view the Exisiting Template's Word Document due to the view Object being greyed out. See below:
I really do not want to copy all my Macros and my quick texts over to a new document just to add one line of text and a button.
I can easily add the required text and CommandButton by having a script run in a Document_New Sub under the ThisDocument. The problem is that this Sub bombs out due to Macros not being enabled. I intend to have the Template distributed to numerous colleagues, therefore having the Text and button on the document will avoid any confusion as to what to do with the template.
You need to open the Template via File - Open
Opening the Template this way will display give you access to the Template.
For info: The name of the Template will appear in the Center of the Word Application.
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.
I am creating a template which has some macros inside. What the macros do is to open a user form which will prompt the user for some information which will then go into the new document.
The problem is that once the new document is created, it still somehow have a reference to this template and the macro code.
I have to send the document to people outside of the organization and they get a warning that the document contains macros.
Is there a way to avoid this? I want the document to be created without any of the macros or referrence to the original template.
Try creating the new document with "Normal" as a parameter for the Documents.Add method. The default seems to be to create a document off of the current open template (or Normal if none).