how can I get the correct name of a word template

H

henry

Hello,
I have tried to get the template name using Document.get_AttachedTemplate()
but i keep on getting normal.dot for all the documents that i have tried to
open which i am sure is not the name because when you open these documents
using microsoft word application and check the name of the attached template
(Tools - Templates and Add-Ins), it gives you the original name of these
documents. I will like to know if and how to get the original template name
of a document programmatically.

Thanks for your time nad assistance
 
G

Graham Mayor

Dim sTemplate As String
With ActiveDocument
sTemplate = .AttachedTemplate.FullName
MsgBox sTemplate
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

henry

thanks Graham for your assistance but I have tried that method already in C#.
for some reason I can use Document.AttachedTemplate so I decided to use
Document.get_AttachedTemplate. However, both method call are thesame but they
always return Normal.dot template which is not what i am looking for. The
documents that I am working on, have thier template on a server and you can
see the correct path when using Microsoft Word Application. In my case, I am
looking for a way to extract those paths programatically. Hope I made my
point clear? Please, I will appreciate any help and I will be happy to help
with more clarificationif needed. thank you.
 
G

Graham Mayor

I have no documents that I have examined that do not show the correct
template path when used with this macro?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

henry

Thanks for your help thou my problem still persist . However, i read a
response given to a similar question like mine. Apparently, in VB I coud get
the name in the template dialog tool box using
Application.Dialogs(wdDialoToolsTemplates).template but I can't seem to get
similar result in C#. Any ideas on how to get around this? Thank you.
 
G

Gordon Bentley-Mix

Henry/Graham,

I think I understand the source of the problem, which may help you to find a
solution - altho I don't have one myself as I'm not a C# programmer. Maybe if
I can explain what's happening, a more savvy contributor can provide an
answer.

If Word does not have access to the template on which a document is based,
it automatically attachs the template to Normal.dot. However, it does not
update the value displayed in the 'Document template' box in the Templates
and Add-ins dialog box; the original value remains. I suspect that the
documents you're attempting to work with have been "disconnected" from their
template and Word has linked them to the Normal instead, which is why
..AttachedTemplate.FullName doesn't provide a valid result.

I ran into a similar problem when trying to develop "rerun" code for a suite
of templates. I wanted to provide a generic rerun function as part of a
global add-in, but the code that needed to be rerun was carried in the
template on which the document was based. All users had access to both the
add-in and the base template, but because each user had their own copy of the
templates, the path to the template varied between users. Consequently, if
one user sent a document to another and the second user attempted to rerun
the template, the code failed because it could not find the attached template
as specified in the Templates and Add-ins dialog box, which includes the
path. I needed to find a way to get my code to call the code in the template
on the user's machine.

The solution was to parse the value in the 'Document template' box to
determine the actual template name (sans path) and pass that to the rerun
code. This worked a treat, and the code now only fails (gracefully) if the
specified template cannot be found on the user's machine - because it's been
deleted or otherwise modified.

I don't know how this information might help you, but at least you can be
assured that you are on the right track. The information you want to retrieve
is in the 'Document template' box in the Templates and Add-ins dialog. Now
it's just a matter of using C# to get it out.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
H

henry

Thanks Gordon, your information is quite helpfull and clear but the problem
now is how do extract the the original template name from WordToolTemplates
dialog. I have tired this code but i could go as far as retireving the dialog.

Word.Dialog d = Application.Dialogs[Word.WdWordDialog.wdDialogToolsTemplates]
Maybe, I could do
d.DefaultTab = Word.wdWordDialogTab.wdDialogTemplates;

This is as far as I think I could go. Please, is there a way of extracting
the name of the template and how? Thank you.
 
G

Gordon Bentley-Mix

Henry,

I'll have to look in my add-in to see what I did. Unfortunately, I've just
upgraded computers, and it seems that the add-in got left behind in the move.
I'll track it down tonight and post back tomorrow.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


henry said:
Thanks Gordon, your information is quite helpfull and clear but the problem
now is how do extract the the original template name from WordToolTemplates
dialog. I have tired this code but i could go as far as retireving the dialog.

Word.Dialog d = Application.Dialogs[Word.WdWordDialog.wdDialogToolsTemplates]
Maybe, I could do
d.DefaultTab = Word.wdWordDialogTab.wdDialogTemplates;

This is as far as I think I could go. Please, is there a way of extracting
the name of the template and how? Thank you.

Gordon Bentley-Mix said:
Henry/Graham,

I think I understand the source of the problem, which may help you to find a
solution - altho I don't have one myself as I'm not a C# programmer. Maybe if
I can explain what's happening, a more savvy contributor can provide an
answer.

If Word does not have access to the template on which a document is based,
it automatically attachs the template to Normal.dot. However, it does not
update the value displayed in the 'Document template' box in the Templates
and Add-ins dialog box; the original value remains. I suspect that the
documents you're attempting to work with have been "disconnected" from their
template and Word has linked them to the Normal instead, which is why
.AttachedTemplate.FullName doesn't provide a valid result.

I ran into a similar problem when trying to develop "rerun" code for a suite
of templates. I wanted to provide a generic rerun function as part of a
global add-in, but the code that needed to be rerun was carried in the
template on which the document was based. All users had access to both the
add-in and the base template, but because each user had their own copy of the
templates, the path to the template varied between users. Consequently, if
one user sent a document to another and the second user attempted to rerun
the template, the code failed because it could not find the attached template
as specified in the Templates and Add-ins dialog box, which includes the
path. I needed to find a way to get my code to call the code in the template
on the user's machine.

The solution was to parse the value in the 'Document template' box to
determine the actual template name (sans path) and pass that to the rerun
code. This worked a treat, and the code now only fails (gracefully) if the
specified template cannot be found on the user's machine - because it's been
deleted or otherwise modified.

I don't know how this information might help you, but at least you can be
assured that you are on the right track. The information you want to retrieve
is in the 'Document template' box in the Templates and Add-ins dialog. Now
it's just a matter of using C# to get it out.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Henry,

Below is the relevant code from my add-in for extracting the template name
from the 'Attached template' box in the Templates and Add-ins dialog box.

Dim FullATName As String
Dim FullATNameLength As Integer
Dim Counter As Integer
Dim CurrentValue As String
Dim Temp As String
FullATName = Dialogs(wdDialogToolsTemplates).Template
FullATNameLength = Len(FullATName)
For Counter = FullATNameLength To 1 Step -1
CurrentValue = Mid(FullATName, Counter, 1)
If CurrentValue = "\" Then Exit For Else Temp = CurrentValue &
Temp
Next Counter

Obviously there's more to it than just this, and in my application, I'm only
interested in the actual file name without the path - thus the reason for
exiting when a backslash is encountered. But the key bit is that I'm working
with the .Template property of the wdDialogToolsTemplates Dialog object.
Unfortunately, this appears to be the where you ran into a roadblock
previously.

~sigh~

Sorry I can't be more help, but I make my living in VBA; I'm completely out
of my depth with C#...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


henry said:
Thanks Gordon, your information is quite helpfull and clear but the problem
now is how do extract the the original template name from WordToolTemplates
dialog. I have tired this code but i could go as far as retireving the dialog.

Word.Dialog d = Application.Dialogs[Word.WdWordDialog.wdDialogToolsTemplates]
Maybe, I could do
d.DefaultTab = Word.wdWordDialogTab.wdDialogTemplates;

This is as far as I think I could go. Please, is there a way of extracting
the name of the template and how? Thank you.

Gordon Bentley-Mix said:
Henry/Graham,

I think I understand the source of the problem, which may help you to find a
solution - altho I don't have one myself as I'm not a C# programmer. Maybe if
I can explain what's happening, a more savvy contributor can provide an
answer.

If Word does not have access to the template on which a document is based,
it automatically attachs the template to Normal.dot. However, it does not
update the value displayed in the 'Document template' box in the Templates
and Add-ins dialog box; the original value remains. I suspect that the
documents you're attempting to work with have been "disconnected" from their
template and Word has linked them to the Normal instead, which is why
.AttachedTemplate.FullName doesn't provide a valid result.

I ran into a similar problem when trying to develop "rerun" code for a suite
of templates. I wanted to provide a generic rerun function as part of a
global add-in, but the code that needed to be rerun was carried in the
template on which the document was based. All users had access to both the
add-in and the base template, but because each user had their own copy of the
templates, the path to the template varied between users. Consequently, if
one user sent a document to another and the second user attempted to rerun
the template, the code failed because it could not find the attached template
as specified in the Templates and Add-ins dialog box, which includes the
path. I needed to find a way to get my code to call the code in the template
on the user's machine.

The solution was to parse the value in the 'Document template' box to
determine the actual template name (sans path) and pass that to the rerun
code. This worked a treat, and the code now only fails (gracefully) if the
specified template cannot be found on the user's machine - because it's been
deleted or otherwise modified.

I don't know how this information might help you, but at least you can be
assured that you are on the right track. The information you want to retrieve
is in the 'Document template' box in the Templates and Add-ins dialog. Now
it's just a matter of using C# to get it out.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
H

henry

Hello Gordan,

Thanks a lot for your effort and assistance. I will study your code closely.

regards
Henry


Gordon Bentley-Mix said:
Henry,

Below is the relevant code from my add-in for extracting the template name
from the 'Attached template' box in the Templates and Add-ins dialog box.

Dim FullATName As String
Dim FullATNameLength As Integer
Dim Counter As Integer
Dim CurrentValue As String
Dim Temp As String
FullATName = Dialogs(wdDialogToolsTemplates).Template
FullATNameLength = Len(FullATName)
For Counter = FullATNameLength To 1 Step -1
CurrentValue = Mid(FullATName, Counter, 1)
If CurrentValue = "\" Then Exit For Else Temp = CurrentValue &
Temp
Next Counter

Obviously there's more to it than just this, and in my application, I'm only
interested in the actual file name without the path - thus the reason for
exiting when a backslash is encountered. But the key bit is that I'm working
with the .Template property of the wdDialogToolsTemplates Dialog object.
Unfortunately, this appears to be the where you ran into a roadblock
previously.

~sigh~

Sorry I can't be more help, but I make my living in VBA; I'm completely out
of my depth with C#...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


henry said:
Thanks Gordon, your information is quite helpfull and clear but the problem
now is how do extract the the original template name from WordToolTemplates
dialog. I have tired this code but i could go as far as retireving the dialog.

Word.Dialog d = Application.Dialogs[Word.WdWordDialog.wdDialogToolsTemplates]
Maybe, I could do
d.DefaultTab = Word.wdWordDialogTab.wdDialogTemplates;

This is as far as I think I could go. Please, is there a way of extracting
the name of the template and how? Thank you.

Gordon Bentley-Mix said:
Henry/Graham,

I think I understand the source of the problem, which may help you to find a
solution - altho I don't have one myself as I'm not a C# programmer. Maybe if
I can explain what's happening, a more savvy contributor can provide an
answer.

If Word does not have access to the template on which a document is based,
it automatically attachs the template to Normal.dot. However, it does not
update the value displayed in the 'Document template' box in the Templates
and Add-ins dialog box; the original value remains. I suspect that the
documents you're attempting to work with have been "disconnected" from their
template and Word has linked them to the Normal instead, which is why
.AttachedTemplate.FullName doesn't provide a valid result.

I ran into a similar problem when trying to develop "rerun" code for a suite
of templates. I wanted to provide a generic rerun function as part of a
global add-in, but the code that needed to be rerun was carried in the
template on which the document was based. All users had access to both the
add-in and the base template, but because each user had their own copy of the
templates, the path to the template varied between users. Consequently, if
one user sent a document to another and the second user attempted to rerun
the template, the code failed because it could not find the attached template
as specified in the Templates and Add-ins dialog box, which includes the
path. I needed to find a way to get my code to call the code in the template
on the user's machine.

The solution was to parse the value in the 'Document template' box to
determine the actual template name (sans path) and pass that to the rerun
code. This worked a treat, and the code now only fails (gracefully) if the
specified template cannot be found on the user's machine - because it's been
deleted or otherwise modified.

I don't know how this information might help you, but at least you can be
assured that you are on the right track. The information you want to retrieve
is in the 'Document template' box in the Templates and Add-ins dialog. Now
it's just a matter of using C# to get it out.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


:

Thanks for your help thou my problem still persist . However, i read a
response given to a similar question like mine. Apparently, in VB I coud get
the name in the template dialog tool box using
Application.Dialogs(wdDialoToolsTemplates).template but I can't seem to get
similar result in C#. Any ideas on how to get around this? Thank you.

:

I have no documents that I have examined that do not show the correct
template path when used with this macro?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


henry wrote:
thanks Graham for your assistance but I have tried that method
already in C#. for some reason I can use Document.AttachedTemplate so
I decided to use Document.get_AttachedTemplate. However, both method
call are thesame but they always return Normal.dot template which is
not what i am looking for. The documents that I am working on, have
thier template on a server and you can see the correct path when
using Microsoft Word Application. In my case, I am looking for a way
to extract those paths programatically. Hope I made my point clear?
Please, I will appreciate any help and I will be happy to help with
more clarificationif needed. thank you.

:

Dim sTemplate As String
With ActiveDocument
sTemplate = .AttachedTemplate.FullName
MsgBox sTemplate
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

henry wrote:
Hello,
I have tried to get the template name using
Document.get_AttachedTemplate() but i keep on getting normal.dot for
all the documents that i have tried to open which i am sure is not
the name because when you open these documents using microsoft word
application and check the name of the attached template (Tools -
Templates and Add-Ins), it gives you the original name of these
documents. I will like to know if and how to get the original
template name of a document programmatically.

Thanks for your time nad assistance
 

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