Ask me to start programming and debug Microsoft Office automation

I use Microsoft Office 2003 and create a bunch of template documents to standardize some tasks. I asked about it on Superuser.com and didnโ€™t get an answer, so I think it is too programmatic and I hope that Iโ€™m more lucky.

I need to automate a workflow that uses a bunch of Office templates (mostly Word). I want to have "My Template Foo.dot" and "My Template Bar.dot" etc. In the "My Foo Bar Stuff" on the shared drive and double-click on the template to create a new Foo or Bar.

I would really like for the user to double-click on the Foo template and receive a request for a couple of elements related to their task (for example, the project number) and have a script in the template change name that "Save" will default to "Foo for Project 1234.doc. "

I asked Google Groups and got an answer that worked ... for a while. Then my AutoNew macro stopped when I created a new document by double-clicking on the template. I have no idea why and how to debug it.

In the class modules / this application, I have:

 Sub AutoNew() Dim Project As String Project = InputBox("Enter the Project Number") ActiveDocument.SaveAs "Project " & Project & " Notes.doc" End Sub 

In Microsoft Word Objects / ThisDocument, I have:

 Private Sub Document_New() End Sub 

I really donโ€™t know why and where it came from.

In Tools / Macro Security ... I have set the security level to Low.

I am a software developer with over 25 years of experience, but full Office noob automation. Specific solutions and pointers to โ€œthis is how to automate Wordโ€ are common. Thanks.


Update:. If I create a new template (new ..., "empty document", "Save as" "My New Template.dot") and insert the AutoNew() macro, it works. So what is stopping him from working on my existing template?

Update 2: Removing a module and function from an old template and adding it back also works.

+4
source share
2 answers

You can attach a template to a saved document in ordre to access the macros contained in the corresponding template.

You can do this by using the AttachedTemplate property of the Document object (i.e. ActiveDocument). Please note that I have not tried it myself.

 Sub AutoNew() Dim Project As String Project = InputBox("Enter the Project Number") ActiveDocument.SaveAs "Project " & Project & " Notes.doc" ActiveDocument.AttachedTemplate = "\\path\to\templates\My Template Foo.dot" End Sub 

See MSDN - VBA Word 2003 Language Reference - AttachedTemplate Property

Hope this helps.

+1
source

Check if the macro is saved in your template. It sounds silly, but it happened to me in Word 2003 under the following circumstances:

  • Create a template containing a macro, everything is fine
  • Create a new macro-based document file, macro kicks in
  • Note that I could improve the template here and there, I do this in a file created in 2) and SaveAs.DOT
  • Macro in .DOT GONE!

Why? Since the code stored in .DOT does not go to the doc file.

0
source

All Articles