Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Seems Easy, Attach a Template, but...

Posted on 2004-10-07
8
Medium Priority
?
937 Views
Last Modified: 2011-10-03
Ok, I've slogging through all the entries out there on templates and I can't find one precisely like the problem I'm having...

There are a bunch of documents in a directory that need to use the same code.
I have a template in the SAME directory as all of these documents.
I want that when a document in that directory opens, it attaches the template to itself.

Sounds easy, but...

This directory is being delivered electronically to lots of different people who will put that directory wherever the heck they want to.  I have no idea if they use local templates or templates on their server, and do not want to have to care where they keep their templates.

So how do I get it to attach the "MyTemplate.dot" file that's in the same folder as the document itself.

I tried the following code:

Private Sub Document_Open()
    With ActiveDocument
        .UpdateStylesOnOpen = True
        Debug.Print ActiveDocument.Path & "\DEP.dot"
        .AttachedTemplate = ActiveDocument.Path & "\DEP.dot"
    End With
End Sub

and get the following error when I open the document:

Microsoft Visual Basic
Run-time error '5947'
Could not change document template.

What am I doing wrong?

250 'cause it's difficult to me!
0
Comment
Question by:epuglise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Author Comment

by:epuglise
ID: 12254867
oops i forgot to get my debug statement out of there... how embarassing!
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 12254987
You will need to write the profile string to the registry. Then you can call it via VBA.  That way, wherever their default template directory is set, it will point to that.  Does that work for you?
0
 

Author Comment

by:epuglise
ID: 12255042
uh... huh? duh... duhm word user here...

I've never written "the profile string to the registry"... does this mean I have to also automatically save the template there?  

What I'm trying to do is give users a folder of stuff they can use without having to move files around.  Probably 3/4s of the users will not know a thing about templates and the 1/4 that do will not want me monkeying with whatever they _are_ doing with them.  I was hoping just to have the template in the same directory and then my code would just be in one place and the users wouldn't have to do anything.

I thought about attaching the code directly to each document (there are 31)... then realized what a mistake that was the first time I changed the code LOL!! (duhm user again!)

so I guess I don't know till you explain the registry thing to me :)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 12255422
If the template is in the same directory as the files, then are they stored to the master template directory?  I certainly meant no insult Elizabeth.

What happens is that in VBA, you find out where the user's default template directory is and then you call a template from that location.  You write the path, etc. to the registry.  Then you can call that path whenever you need it.  Something like this:

'write value to registry
System.PrivateProfileString("", "HKEY_CURRENT_USER\Software\Microsoft\Letter", "Database") = temp

'call template from that directory
Documents.Add Template:=Options.DefaultFilePath(wdUserTemplatesPath) & "\Env.DOT", newtemplate:=False

I could be totally offbase, but I'm pretty sure it would work.  





0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 total points
ID: 12257606
If I were to write a macro, I would write one to copy the template to the startup folder.

    FileCopy ActiveDocument.Path & "\dep.dot", Application.Options.DefaultFilePath(wdStartupPath) & "\dep.dot"

Where to put the macro though - Normal template, dep template or in each document?

If it's to go in Normal, it would have to go in all the end-users' templates. If in each document, then while you're about it you might as well insert all the code that is in the dep template. If it's in the dep template, then how do you get it to run?

However, if the document is already attached to a template called 'dep' before it is sent, then, if, when Word opens the document, it can't find a template of that name in the templates folder and sub-folders, it will search in the same folder as the document and attach it if it is found there.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 12263444
If you are the one sending the documents to everyone, I assume the template is already attached to all those documents.  You could put a line in of code in an AutoOpen or AutoNew macro in each document such as Graham suggested, although I think the code you had was already almost there.  Perhaps you were just struggling with the location of the template:

Option Explicit
Dim SDir As String
Sub AutoOpen()
   sDir = ActiveDocument.Path
   With ActiveDocument
        .UpdateStylesOnOpen = True
        .AttachedTemplate = SDir & "\dep.dot"
        'I always turn this off because it will cause problems with styles - this is a matter of preference
        .UpdateStylesOnOpen = False
    End With
End Sub


You would do the same for AutoNew.  This works at my PC.  Hope it's not too burdensome to put the code in each document.....
 
   
0
 

Author Comment

by:epuglise
ID: 12267386
Jo:  there was never any offense... I just wanted you to know that you are dealing with a very novice user and what you suggested to me (messing with the registry!!) sounded really scary...

Graham:  Just to be clear.
1. I have already attached the DEP template to each of the documents.
2. If I have all of the documents and the DEP template in the same folder I distribute to folks (and they don't move anything out of the folder)... then is it true that

3.  I do not need any code and Word will automatically look for the template (after looking other places)... in the same directory from which the document was opened??

I would LOVE a solution that requires zero code in the documents themselves...
0
 

Author Comment

by:epuglise
ID: 12267449
I think I was trying too hard...  I didn't realize word would look for the template automatically in the same place as the document... learn something new every day, I guess!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question