Open New Document with same template

L

Liz

Hi -
I am looking for a good way to open a new document from an open
document...where the new document is based on the same template of the
already open document. I am trying to avoid using a hardcoded template in my
vba code, and make it a variable based on whatever template the user is
using....

For instance...all I have is this....which is hardcoded...

Sub openTemplate()

Word.Documents.Add Template:="C:\path\To\My\Template.dot"

End Sub


Thanks,
Liz
 
P

Pesach Shelnitz

Hi Liz,

The following macro does this. Note that I included the different code
needed for Word 2007 and Word 2003. The code for Word 2003 is commented out.
Adjust the commenting as necessary.

Sub NewFileWithCurrentTemplate()
Dim templateName As String
Dim templatePath As String

' For Word 2007
templatePath = Environ$("SystemDrive") & "\Users\" _
& Environ$("USERNAME") & "\AppData\Roaming\Microsoft\Templates\"
' For Word 2003
'templatePath = Environ$("SystemDrive") & "\Documents and Settings\" _
' & Environ$("USERNAME") & "\Application Data\Microsoft\Templates\"
templateName = ActiveDocument.AttachedTemplate
Documents.Add Template:=templatePath & templateName
End Sub
 
J

Jay Freedman

Please allow a correction and a suggestion...

The difference in the code isn't for different versions of Word but for
different versions of Windows. The "Documents and Settings" folder exists on
Windows XP and Windows Server 2003; it's replaced by the "Users" folder on
Windows Vista, Windows Server 2008, and Windows 7. Either version of Word can
run on any of those Windows versions.

A better solution is to use the "AppData" environment string, which points to
the current profile's Application Data folder on the earlier Windows versions
and to the AppData\Roaming folder on the later Windows versions, without having
to know which version it is. This single line will replace both variations in
the macro:

templatePath = Environ$("AppData") & "\Microsoft\Templates\"

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
T

Tony Jollans

If I may, I'd like to correct both you gentlemen. You are hard coding an
assumption - about where the template is. This is easier and should work in
all versions of Word and all versions of Windows:

Documents.Add ActiveDocument.AttachedTemplate.Fullname

(replace ActiveDocument with a reference to the whatever the 'base' document
is)
 
J

Jay Freedman

Touché. <g>

If I may, I'd like to correct both you gentlemen. You are hard coding an
assumption - about where the template is. This is easier and should work in
all versions of Word and all versions of Windows:

Documents.Add ActiveDocument.AttachedTemplate.Fullname

(replace ActiveDocument with a reference to the whatever the 'base' document
is)

--
Enjoy,
Tony

www.WordArticles.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top