Accessing of Macros in word...Tricky one?

Ok the title is a little deceptive but I'm sure the solution is macro related. A small firm has asked me to have a look at this issue. #
Using a new doc managment system, every time they create a blank document through this system the document is already saved and named and added to the system, ok , fine. So when they go to use a template it creates a new document (not created from within the doc managment system) and thus when saved does not show up in the doc manager console.

Is there anyway that when you open a template it will transfer to the blank document originally opened.
At the moment the user goes to file-New-->(Templates) On My Computer-->chooses a template a new document opens. So is there anyway I can get this template to open in the already saved and named blank document that is open.

Does that make any sense!!!Sorry...any help would be greatly appreciated.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Mark_FreeSoftwareConnect With a Mentor Commented:

you can create a macro that runs when a document is open.

to do so, open a new document

press alt + f8

type a name in the field, and press Create (or somthing like that, i'm not on an english word, so it could be a little different)
let's assume you used this name : "TemplateRunMacro" (without the quotes ofcourse)

now the editor will pop up
expand the item Normal (or whichever the name is of the template to attach to)
expand Microsoft Word -objects
doubleclick on "ThisDocument"

now paste this code:

Private Sub Document_Open()
  call TemplateRunMacro
End Sub

if you save it, the sub TemplateRunMacro will be executed every time a document is opened


you could include a macro in the templates, that find an existing word app,
and paste all contents of the template in it.

but you _have_ to do some checks, to see if the user has not got 3 or more instances of word running
yes, u can attach a template to an already open and saved document by using the following command

ActiveDocument.AttachedTemplate = "C:\<template name>.dot"

your question is not very clear to tell u where exactly u should insert this code.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

It is hard to match your description to what Word does.

Usually a user does not 'Open' a template. A developer is more lilkey to do that. The template is used to create a new document, and it will be the document's Attached Template. Many times the template will be the Normal template.

You can change the attached template via the Word User interface (Tools/Templates and Add-ins) or programatically as
gafoorgk suggests. You would then be able to use the new template's styles, macros, custom toolbars, etc. But if the template had some document text, hopefully nicely formatted, then that would not appear. It would have to be copied to the new document.

It sounds as if what you want any new document to be forced to take up the name of the pre-existing 'New' document that your document management system has already assigned. It might just be possible to do that, provided that there is a programmatic way of finding out whether such a blank document exists and what its name is.

However, it sounds like a fairly predictable conundrum, and one that the designers of the system would have considered, so they may have a ready answer. Perhaps their literature and/or support should be consulted.
thomaskilroyAuthor Commented:
Guys thanks alot for your help I appreciate it, ok. Sorry for not explaining myself properly (Mark_FreeSoftware  seems to be closest to what I'm looking for).

The software is a legal case management package. At the moment all this firms precedents are in template form in word but I wish to include all created documents in the new package. So what I would wish to happen is:
1. Uses chooses blank document from within software package, this opens a blank word document ( letterNo13.doc) which is automatically saved to the system. Now my users are accustomed to going to file-->new and choosing a precedent template, but obviously this opens a new document.

What I was wondering is when they choose this template is it possible for its contents to be placed in the already saved blank document opened from the case management software.

I understand that this seems very convoluted and any suggestions of achieving this by other means would be greatly appreciated, its just that the majority of people using this system are not very computer 'savvy' and the old way is the best way...thanks again guys

Mark how might I code this ?
thomaskilroyAuthor Commented:
Hello!!? can anyone help me with this..........

>>What I was wondering is when they choose this template is it possible for its contents to be placed in the already saved blank document opened from the case >>management software.

you could edit the default template,

and you can also have a document execute a macro when it is opened (even for new documents)

which one do you want more info about?

(and pls give me a hint of your knowledge of word/macro's so i know to what level of detail to explain to
GrahamSkanConnect With a Mentor RetiredCommented:
The simplest thing to do would be to save the new document with the document name of the one created by your document management system.

Option Explicit

Private Sub Document_New()
    ActiveDocument.SaveAs "C:\MyFolder\letter13.doc"
End Sub

Whatever we do, we need to know the name of the blank document from the document management system. Is there a rule for that?
thomaskilroyAuthor Commented:
AAAAAAHHHHHH I just spent an hour writing a reply and it was lost I've never been more frustrated with this sh*T f**k.........
thomaskilroyAuthor Commented:
ok...thanks for the reply guys...ggggrrrrrrr..........I appreciate it. Mark my experience is very limited so I'm not sure what the best option is just to go over it.

Here again is the scenario:
1. User opens word from within Case Management software, they are requested to give the doc a name, which is saved automatically.
2. Now there are 60 templates on this users computer so user now wants to use one of these 60 templates.

How would the user be able to access any one of these templates and have it's contents place in the already saved and open, blank document referred to in Step 1 above?
It is too impractical to have 10 different toolbars etc. so I am unsure of how to do this?

Graham,cheers for your time,
 I don't think that will work because everytime a document is generated from a template, the user will be asked would they like to replace the existing document. I know this will confuse our older users. It adds a layer of complexity I want to avoid.

Thanks Guys.


can't they just open a template, and then save it somewhere, instead first saving somewhere and than opening the template?
VBA .SaveAs shouldn't prompt to overwrite. The difficulty is in finding the document that the Case Management software has just created. How do the users find it again? Can we do that programatically?
thomaskilroyAuthor Commented:
Great guys thanks for your help

The folder scheme is based on client number and case numbers, so saving files to these folders is far from intuitive,it would be very hard to find the correct client and then the correct case folder.

""can't they just open a template, and then save it somewhere, instead first saving somewhere and than opening the template?""
Alas no, to be honest if they could this would be a mute question. Unfortunately, this is the chain of events that must occur."

""The difficulty is in finding the document that the Case Management software has just created. How do the users find it again?""
The users will find the documents by finding the correct case in the Case Management system, the files pertaining to the specific case will be listed. The idea is to keep the user out of the file tree structure on the application server.

Thanks again guys.........
Surely the application will allow the user to choose the template at the initial creation time.
This one seems to:
thomaskilroyAuthor Commented:
Thanks for the prompt reply Graham,
THe application does allow this, but this company does not want all its templates coded within the case management system. Instead they wish to be able to choose them from within word. If this could be done another way I would do it! Unfortunately this is the preferred way for this company.

Any ideas?
thomaskilroyAuthor Commented:
Going back to your original post MArk:

""you could include a macro in the templates, that find an existing word app,
and paste all contents of the template in it.""

How might this be done?

thomaskilroyAuthor Commented:
Hi Guys,
I think I may of found a ridiculously easy way around this issue. By going to 'Insert File' in the Insert menu of Word. So I think I'm closer to the solution. Guys, is there a macro that would run when you try to open a template it would replicate insert file and place the contents in a new document?
Thanks again for your time.
Will there only be one 'template' to load from?

If so, the only problem would be to make sure that it only happens once. This could be avoided by setting a document variable.

Note that this code would have to be in the document that is being opened.

Private Sub Document_Open()
    Dim v As Variable
    Dim rng As Range
    For Each v In ThisDocument.Variables
        If v.Name = "TemplateLoaded" Then
            Exit Sub
        End If
    Next v
    Set rng = ThisDocument.Range
    rng.InsertFile "C:\MyFolder\MyTemplate.doc"
    ThisDocument.Variables.Add "TemplateLoaded"
End Sub

thomaskilroyAuthor Commented:
Thanks guys, I really appreciate it. I will try it Monday morning....have a great weekend


split between GrahamSkan (300 pts)
and me (200 pts)
All Courses

From novice to tech pro — start learning today.