Customization Context

H

Hichem S

Hi,
I am deleting the commandbars I create when loading the word add-in
developed with c#. As I understood, word does not take into account the
temporary properties of commanbars => so my commandbars are not deleted.
I konw that I need to use the CustomizationContext property.
The only problem is that I create my commandbars before any document is
created.
I it possible to set the CustomizationContext after the commandbars are
created?

Also, in some threads we talk about the ActiveDocument.AttachedTemplate
property.. but i don't find it?

Thank you for ur help...
 
J

Jonathan West

Hichem S said:
Hi,
I am deleting the commandbars I create when loading the word add-in
developed with c#. As I understood, word does not take into account the
temporary properties of commanbars => so my commandbars are not deleted.

Correct. This is a longstanding bug in Word.
I konw that I need to use the CustomizationContext property.
The only problem is that I create my commandbars before any document is
created.
I it possible to set the CustomizationContext after the commandbars are
created?

In the absence of any setting otherwise, the CustomizationContext is
NormalTemplate, and this is where your changes are stored.

I would recommend you don't to store your menu & toolbar changes in
normal.dot. Too much scope for things to go wrong. Instead, I would
recommend that you create a global template (add-in). Make sure the AddIn is
made a member of the Addins collection and is loaded. Then you can set the
CustomizationContext to that add-in and apply the toolbar changes there.

When you unload your C# add-in, set the Saved property of the AddIn to True
and then unload it. The changes you made to its menus & toolbars will be
discarded.
Also, in some threads we talk about the ActiveDocument.AttachedTemplate
property.. but i don't find it?

The ActiveDocument is the document whose editing window is open and active
at present. ActiveDocument is an object of type Document. AttachedTemplate
is a property of any Document object, and represents the Template object
that the document is based on.
 
C

Cindy M.

Hi =?Utf-8?B?SGljaGVtIFM=?=,
I am deleting the commandbars I create when loading the word add-in
developed with c#. As I understood, word does not take into account the
temporary properties of commanbars => so my commandbars are not deleted.
I konw that I need to use the CustomizationContext property.
The only problem is that I create my commandbars before any document is
created.
I it possible to set the CustomizationContext after the commandbars are
created?

Also, in some threads we talk about the ActiveDocument.AttachedTemplate
property.. but i don't find it?
In C# you need the get_AttachedTemplate method.

As to the other part of your question...

NormalTemplate should be available when your add-in loads. There is a
question whether you should be putting your customizations in
NormalTemplate, as this is actually for the user.

Good practice would be to create (or distribuate) a template with your Add
-in, load that as an Add-in (the template kind) then use that as the
CustomizationContext.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
J

Jonathan West

Good practice would be to create (or distribuate) a template with your Add
-in, load that as an Add-in (the template kind) then use that as the
CustomizationContext.


Snap :)
 
H

Hichem S

Hi Cindy & Jonathan,
Thank you both for your answers...
What I should do then, is to create a template (an empty file with .dot
extension?) that will be used as follow? :

((Word.Application)application).AddIns.Add("C:\\MyTemplate.dot", false);

((Word.Application)application).CustomizationContext =
((Word.Application)application).AddIns.Item(NameOfTemplate);

This is just the idea but the code does not work. Do you have a correct code
that works? "Add" method needs a bool or a ref to an object?? Is the Item or
get_item that should be used? what are the parameters?

Thank you for your help..
 
C

Cindy M.

Hi Hichem,
What I should do then, is to create a template (an empty file with .dot
extension?) that will be used as follow? :

((Word.Application)application).AddIns.Add("C:\\MyTemplate.dot", false);

((Word.Application)application).CustomizationContext =
((Word.Application)application).AddIns.Item(NameOfTemplate);

This is just the idea but the code does not work. Do you have a correct code
that works? "Add" method needs a bool or a ref to an object?? Is the Item or
get_item that should be used? what are the parameters?
It always helps if you give more detail about HOW it "doesn't work". (Which
line of code, what the error is...)

I'm just getting ready to close down for the night, so setting up a test
enviroment to work out the exact code isn't going to happen immediately :)
However.

Customizationcontext requires a Document or Template object. Since C# demands
strict data typing, you have to "convert" the Add-in object to a template
object. I'd try something along the lines of:

Word.Template tplt = Word.Application.Templates("C:\\MyTemplate.dot")

OR (probably a better than relying on a file path)

Word.Template tpltAddin = null;
foreach (Word.Template tplt in wdApplication.Templates)
{
if (tplt.Name.Contains("MyTemplate"))
{
tpltAddin = tplt;
}
}
if (tplt!=null)
{
wdApplication.CustomizationContext = tplt;
}

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
H

Hichem S

Good morning Cindy!
Thank you for the answer.
This what i do right now :
foreach (Word.Template tplt in _wordApplication.Templates)
{
if (tplt.Name.Contains("MyTemplate"))
{
_wordApplication.CustomizationContext = tplt;
}
}
I created manually the MyTemplate.dot using Word. I also put it manually in
"C:\Documents and Settings\hichem\Application Data\Microsoft\Word\STARTUP" so
that it always loads with Word. Word load it all the time, BUT when I debug,
I always find only one element (Normal.dot) in wordApplication.Templates
collection.
What's wrong...
If what I am doing is right (manually creating the template and make it load
by word) , then how can I do that programatically?
Thank you for your help....
 
C

Cindy M.

Hi =?Utf-8?B?SGljaGVtIFM=?=,
This what i do right now :
foreach (Word.Template tplt in _wordApplication.Templates)
{
if (tplt.Name.Contains("MyTemplate"))
{
_wordApplication.CustomizationContext = tplt;
}
}
I created manually the MyTemplate.dot using Word. I also put it manually in
"C:\Documents and Settings\hichem\Application Data\Microsoft\Word\STARTUP" so
that it always loads with Word. Word load it all the time, BUT when I debug,
I always find only one element (Normal.dot) in wordApplication.Templates
collection.
What's wrong...
If what I am doing is right (manually creating the template and make it load
by word) , then how can I do that programatically?
At what point in your code are you checking the Templates collection? Possibly,
word hasn't finished loading it, yet...

Are you continuing to use ((Word.Application)application).AddIns.Add
("C:\\MyTemplate.dot", false); in your code? This would be the most reliable
way. And I'd probably leave the template file in your program folders and unload
it when your add-in shuts down. Then it can't possibly be "left over" if your
add-in fails, or the user uninstalls the add-in.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
H

Hichem S

Hi,
Cindy and Jonathan, thank you for your comments. I was on holiday
I am posting a code that will help every person who encounter the same
problem...
There were some confusion on how to save the customization context, add the
template to the add-ins collection.
This an example that works! Have fun

Hichem

*************
protected override void SetCustomizationContext()
{
object install = true;
_wordApplication.AddIns.Add(Path.Combine(templatePath,
TEMPLATE), ref install);
Template template = GetCustomWordTemplate();
_wordApplication.CustomizationContext = template;
}

protected override void SaveCustomizationContext()
{
Template template = GetCustomWordTemplate();
if (template != null && !template.Saved)
{
template.Saved = true;
}
}


private Template GetCustomWordTemplate()
{
foreach (Template template in _wordApplication.Templates)
{
if (String.Compare(template.Name, TEMPLATE) == 0)
{
return template;
}
}
return null;
}
**********
 

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