Retrieve a template from template folder using C#

Z

zfeld

I am trying to retrieve a my custom template from the word template
folder using C# however my code seems to only find normal.dot. When I
look at the directory in explorer my template is there. I can retrieve
it if I specify a path to the file,but not using word.templates, which
is what I need. What am I doing wrong. I am new to office automation
so bear with me if it is obvious

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
string templatePath = "";
foreach(Word.Template CurTemplate in oWord.Templates)
{
if (CurTemplate.Name == "My Report.dot")
{
templatePath = CurTemplate.FullName;
break;
}
}
if(templatePath.Length == 0)
return;

object oTemplate = templatePath;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,ref oMissing,
ref oMissing);
 
C

Cindy M -WordMVP-

Hi Zfeld,

The templates collection only encompasses templates that are currently
loaded (template attached to the active document; Normal.dot; any
templates loaded from the STARTUP folder; any templates the user (or
you) have explicitly opened as Addins).

Any templates saved to disk, but not loaded, are not in the Templates
collection. If, however, you're certain that the template you want is in
the same folder with Normal.dot, then you can
- NormalTemplate.FullPath
- Then loop through all the files in that path (using .NET
Framework) until you find the one you want.
- OR, if you already know the name, you now have the path and just
append the name to it and use it for whatever (Documents.add?)
I am trying to retrieve a my custom template from the word template
folder using C# however my code seems to only find normal.dot. When I
look at the directory in explorer my template is there. I can retrieve
it if I specify a path to the file,but not using word.templates, which
is what I need. What am I doing wrong. I am new to office automation
so bear with me if it is obvious

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
string templatePath = "";
foreach(Word.Template CurTemplate in oWord.Templates)
{
if (CurTemplate.Name == "My Report.dot")
{
templatePath = CurTemplate.FullName;
break;
}
}
if(templatePath.Length == 0)
return;

object oTemplate = templatePath;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,ref oMissing,
ref oMissing);

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

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