CommandBarButton not getting deleted in MS Word

S

Sandesh Patnam

« Previous Thread


Today, 6:16 AM UTC
Sandesh Patnam


Posts 2
CommandBarButton not getting deleted in MS Word



I am developing an Add-In for Word/Excel/PowerPoint applications. I am
using Office 2003. In the OnBeginShutDown method, I am trying to delete the
existing button. The button gets deleted for Excel and PowerPoint. But for
Word, it is throwing "Exception from HRESULT: 0x800A01A8" exception.

Please suggest me a fix for this or atleast an alternative to prevent this
exception.



Report Abuse



Today, 6:33 AM UTC
Pavan Kulkarni


Posts 77 Re: CommandBarButton not getting deleted in MS Word
Was this post helpful ?


Hi Sandesh,
This is quite a common issue with Word and you can find solution to this in
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=356963&SiteID=1&mode=1&PageID=0 thread.





Report Abuse



Today, 7:45 AM UTC
Sandesh Patnam


Posts 2
Re: CommandBarButton not getting deleted in MS Word



Hi Pavan,

Thanks for your suggestion. I implemented the solution mentioned in the
thread. But still I am getting the same exception while trying to delete the
button. In addition to that I am getting a confirmation message saying that
"Do you want to save changes made to temp.dot" which is irritative to the end
user.



Report Abuse



Today, 7:58 AM UTC
Cindy Meister
MVP

Posts 496 Re: CommandBarButton not getting deleted in MS Word
Was this post helpful ?


Hi Sandesh

Let's start with the easy part, first: the confirmation message :) You need
to either save the changes to the template, or tell Word not to worry about
saving changes. If you want to save your changes to the template, then use
the SAVE method in your code after you've made the changes.

If you want to throw away the changes, use the SAVED property to indicate to
Word that there's nothing left to be saved.

Beyond that

1. This isn't the correct venue to discuss COM-Addins, really. The
discussion group for that topic is

http://msdn.microsoft.com/newsgroup...ic.office.developer.com.add_ins&lang=en&cr=US

2. We need to see your code for adding the button. My best guess would be
that you aren't specifying the CustomizationContext - telling Word where you
want to create/delete the button.

3. Using the technique described in the message pavan pointed you to, you
wouldn't necessarily NEED to use code to create/delete the button in a
separate template. If this is a button that should always be present when
your Add-in (the template) is loaded, then you can create it in the template
in Word's UI and just leave it there.
 
S

Sandesh Patnam

Hello Cindy,

Thanks for your reply. As you suggested, I just changed the topic to the
discussion group mentioned.

I have tried saving the template but the problem persists still. I following
is the code that I am using for adding the CommandBarButton.

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
wdApp = (Word.Application)application;
wdApp.DocumentChange += new
Word.ApplicationEvents4_DocumentChangeEventHandler(this.WordApp_DocumentChange);
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}

private void WordApp_DocumentChange()
{
if(!Initialized)
{
Word.Template currentTpl = (Word.Template)wdApp.CustomizationContext;
foreach(Word.Template tpl in wdApp.Templates)
{
if(tpl.FullName==@"C:\Program Files\Microsoft
Office\OFFICE11\STARTUP\Planitax.dot") //the name of the template you
installed
{
wdApp.CustomizationContext = tpl;
CommandBars objCommandBars;
CommandBar objCommandBar;
try
{
objCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch
{
object objActiveExplorer;
objActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBars=
(CommandBars)objActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,objActiveExplorer,null);
}
try
{
objCommandBar = objCommandBars["Standard"];
}
catch(Exception)
{
objCommandBar = objCommandBars["Drawing"];
}
// In case the button was not deleted, use the exiting one.
try
{
objButton = (CommandBarButton)objCommandBar.Controls["Planitax
Horizons"];

}
catch
{
Image btnImg;
Form obj = null;
string path = Application.LocalUserAppDataPath;
path = path.Remove(3,path.Length-3);
path = path + @"Program Files\Planitax\Planitax MS Office
Plug-In\Images\PlanitaxHorizonsIcon.png";
try
{
btnImg = System.Drawing.Image.FromFile(path,false);
btnImg = new Bitmap(btnImg,150,50);
}
catch
{
UI.FrmErrorInfo frmErrorInfo = new
PlanitaxWord.UI.FrmErrorInfo("Some of the files or folders are deleted or
moved from their location. Please Re Install the package.");
frmErrorInfo.ShowDialog(obj);
return;
}
object omissing = System.Reflection.Missing.Value;
if(objButton == null)
{
objButton = (CommandBarButton) objCommandBar.Controls.Add(1,
omissing , omissing , omissing , true);

objButton.Caption = "Planitax Horizons";
objButton.TooltipText = "Save to Planitax Horizons";
objButton.Style = MsoButtonStyle.msoButtonIcon;
Clipboard.SetDataObject(btnImg,false);
objButton.PasteFace();
}
}
//The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
objButton.Tag = "Planitax Horizons";
// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
objButton.OnAction = "!<PlanitaxWord.Connect>";
objButton.Visible = true;
objButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(this.objButton_Click);
objName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBar = null;
objCommandBars = null;

Initialized = true;
wdApp.CustomizationContext = currentTpl;
currentTpl.Save();
break;
}
}
}
}

Please review the code and suggest me the changes if any.
 
P

pavvu_kk

Hi Sandesh

In addition to what Cindy has mentioned, do not try to delete the button in
your code. The whole idea of this solution is that

1) when you uninstall your add-in the template is removed and thus the
button doesn't remain.

2) if your requirement is not just the uninstall scenario but to delete
buttons when the host app closes and add when it opens then, you can either
add the new template to the addins collection of word programatically when
the host app loads or install it into the startup folder so that word does
that automatically for you.

3) if your requirement is to add buttons only to few documents and not to
all, then prob you can try the VBA approach. If for some reason you want to
go for add-in approcah, there is sol for the same.

Pls let us know what exactly is your requirement. The thread link that I've
given you has all the info required to implement this approach. It also talks
about how to save the template or how NOT to save it.

Hope this helps


Sandesh Patnam said:
Hello Cindy,

Thanks for your reply. As you suggested, I just changed the topic to the
discussion group mentioned.

I have tried saving the template but the problem persists still. I following
is the code that I am using for adding the CommandBarButton.

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
wdApp = (Word.Application)application;
wdApp.DocumentChange += new
Word.ApplicationEvents4_DocumentChangeEventHandler(this.WordApp_DocumentChange);
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}

private void WordApp_DocumentChange()
{
if(!Initialized)
{
Word.Template currentTpl = (Word.Template)wdApp.CustomizationContext;
foreach(Word.Template tpl in wdApp.Templates)
{
if(tpl.FullName==@"C:\Program Files\Microsoft
Office\OFFICE11\STARTUP\Planitax.dot") //the name of the template you
installed
{
wdApp.CustomizationContext = tpl;
CommandBars objCommandBars;
CommandBar objCommandBar;
try
{
objCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch
{
object objActiveExplorer;
objActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBars=
(CommandBars)objActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,objActiveExplorer,null);
}
try
{
objCommandBar = objCommandBars["Standard"];
}
catch(Exception)
{
objCommandBar = objCommandBars["Drawing"];
}
// In case the button was not deleted, use the exiting one.
try
{
objButton = (CommandBarButton)objCommandBar.Controls["Planitax
Horizons"];

}
catch
{
Image btnImg;
Form obj = null;
string path = Application.LocalUserAppDataPath;
path = path.Remove(3,path.Length-3);
path = path + @"Program Files\Planitax\Planitax MS Office
Plug-In\Images\PlanitaxHorizonsIcon.png";
try
{
btnImg = System.Drawing.Image.FromFile(path,false);
btnImg = new Bitmap(btnImg,150,50);
}
catch
{
UI.FrmErrorInfo frmErrorInfo = new
PlanitaxWord.UI.FrmErrorInfo("Some of the files or folders are deleted or
moved from their location. Please Re Install the package.");
frmErrorInfo.ShowDialog(obj);
return;
}
object omissing = System.Reflection.Missing.Value;
if(objButton == null)
{
objButton = (CommandBarButton) objCommandBar.Controls.Add(1,
omissing , omissing , omissing , true);

objButton.Caption = "Planitax Horizons";
objButton.TooltipText = "Save to Planitax Horizons";
objButton.Style = MsoButtonStyle.msoButtonIcon;
Clipboard.SetDataObject(btnImg,false);
objButton.PasteFace();
}
}
//The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
objButton.Tag = "Planitax Horizons";
// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
objButton.OnAction = "!<PlanitaxWord.Connect>";
objButton.Visible = true;
objButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(this.objButton_Click);
objName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBar = null;
objCommandBars = null;

Initialized = true;
wdApp.CustomizationContext = currentTpl;
currentTpl.Save();
break;
}
}
}
}

Please review the code and suggest me the changes if any.
 
C

Cindy M.

Hi =?Utf-8?B?U2FuZGVzaCBQYXRuYW0=?=,
I have tried saving the template but the problem persists still.
Which problem? Originally, you reported two problems...

I think, given what I see in your code, we also need to know which version of
Word/Office you're targeting.
I following
is the code that I am using for adding the CommandBarButton.
Well, let's start here:

Word.Template currentTpl = (Word.Template)wdApp.CustomizationContext;

It's not said that the customizationcontext is actually a template. It could be
a document. So this line of code is a potential source of errors... And you
don't even have it in a try...catch block. In order to do this correctly you
need to take all possibilities into account. This means you'd need to use a
variable of type "Object": Object currentTpl = (Note: I assume the sole reason
for this is to restore the customization context later. Therefore, in this
instance, you don't need to differentiate whether it's a document or template.)

Beyond that, I don't understand at all what you're trying to do in the
following block of code. Why use InvokeMember with the applicationObject?
You're in a Word event, use WdApp.Commandbars. And Word has no ActiveExplorer
in its object model - I think this is for Outlook. Why are you using it in a
Word event?

{
WdApp.CustomizationContext = tpl;
CommandBars objCommandBars;
CommandBar objCommandBar;
try
{
objCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch
{
object objActiveExplorer;
objActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetPrope
rty,null,applicati
onObject,null);
objCommandBars=
(CommandBars)objActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlag
s.GetPro
perty,null,objActiveExplorer,null);
}

You seem to be relying on try...catch a lot, which is putting a lot of extra
overhead in your code. It's much more efficient to do a test (like loop through
a collection) than wait for the error handling to kick in...

I'm also a bit amazed that your code is not sinking the Click events for the
toolbars, but relying on OnAction...

And finally, you have: currentTpl.Save();
This should be tpl.Save() since that's the template object to which the toolbar
customizations are being made. You'll probably need to declare tpl at the
beginning of the procedure, and not in the foreach loop, to bring it into
scope.

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 :)
 
P

pavvu_kk

Do tpl.Save() instead currentTpl.Save()
You will not get prompt for save. You are saving the wrong template. Your
toolbar is added to tpl and not currentTpl

Sandesh Patnam said:
Hello Cindy,

Thanks for your reply. As you suggested, I just changed the topic to the
discussion group mentioned.

I have tried saving the template but the problem persists still. I following
is the code that I am using for adding the CommandBarButton.

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
wdApp = (Word.Application)application;
wdApp.DocumentChange += new
Word.ApplicationEvents4_DocumentChangeEventHandler(this.WordApp_DocumentChange);
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}

private void WordApp_DocumentChange()
{
if(!Initialized)
{
Word.Template currentTpl = (Word.Template)wdApp.CustomizationContext;
foreach(Word.Template tpl in wdApp.Templates)
{
if(tpl.FullName==@"C:\Program Files\Microsoft
Office\OFFICE11\STARTUP\Planitax.dot") //the name of the template you
installed
{
wdApp.CustomizationContext = tpl;
CommandBars objCommandBars;
CommandBar objCommandBar;
try
{
objCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch
{
object objActiveExplorer;
objActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBars=
(CommandBars)objActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,objActiveExplorer,null);
}
try
{
objCommandBar = objCommandBars["Standard"];
}
catch(Exception)
{
objCommandBar = objCommandBars["Drawing"];
}
// In case the button was not deleted, use the exiting one.
try
{
objButton = (CommandBarButton)objCommandBar.Controls["Planitax
Horizons"];

}
catch
{
Image btnImg;
Form obj = null;
string path = Application.LocalUserAppDataPath;
path = path.Remove(3,path.Length-3);
path = path + @"Program Files\Planitax\Planitax MS Office
Plug-In\Images\PlanitaxHorizonsIcon.png";
try
{
btnImg = System.Drawing.Image.FromFile(path,false);
btnImg = new Bitmap(btnImg,150,50);
}
catch
{
UI.FrmErrorInfo frmErrorInfo = new
PlanitaxWord.UI.FrmErrorInfo("Some of the files or folders are deleted or
moved from their location. Please Re Install the package.");
frmErrorInfo.ShowDialog(obj);
return;
}
object omissing = System.Reflection.Missing.Value;
if(objButton == null)
{
objButton = (CommandBarButton) objCommandBar.Controls.Add(1,
omissing , omissing , omissing , true);

objButton.Caption = "Planitax Horizons";
objButton.TooltipText = "Save to Planitax Horizons";
objButton.Style = MsoButtonStyle.msoButtonIcon;
Clipboard.SetDataObject(btnImg,false);
objButton.PasteFace();
}
}
//The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
objButton.Tag = "Planitax Horizons";
// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
objButton.OnAction = "!<PlanitaxWord.Connect>";
objButton.Visible = true;
objButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(this.objButton_Click);
objName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBar = null;
objCommandBars = null;

Initialized = true;
wdApp.CustomizationContext = currentTpl;
currentTpl.Save();
break;
}
}
}
}

Please review the code and suggest me the changes if any.
 
S

Sandesh Patnam

Pavan,

My requirement is to remove the button during uninstall.

Thats working fine and removing the button after uninstalling it. But now I
am not able to eliminate the pop up window for saving the changes in the
template. Please review the code below.

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;
wdApp = (Word.Application)application;
wdApp.DocumentChange += new
Word.ApplicationEvents4_DocumentChangeEventHandler(this.WordApp_DocumentChange);
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}


private void WordApp_DocumentChange()
{
try
{
if(!Initialized)
{
currentTpl = (Word.Template)wdApp.CustomizationContext;
foreach(Word.Template tpl in wdApp.Templates)
{
if(tpl.FullName==@"C:\Program Files\Microsoft
Office\OFFICE11\STARTUP\Planitax.dot") //the name of the template you
installed
{
wdApp.CustomizationContext = tpl;
CommandBars objCommandBars;
CommandBar objCommandBar;
try
{
objCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch
{
object objActiveExplorer;
objActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBars=
(CommandBars)objActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,objActiveExplorer,null);
}
try
{
objCommandBar = objCommandBars["Standard"];
}
catch(Exception)
{
objCommandBar = objCommandBars["Drawing"];
}
// In case the button was not deleted, use the exiting one.
try
{
objButton = (CommandBarButton)objCommandBar.Controls["Planitax
Horizons"];

}
catch
{
Image btnImg;
Form obj = null;
string path = Application.LocalUserAppDataPath;
path = path.Remove(3,path.Length-3);
path = path + @"Program Files\Planitax\Planitax MS Office
Plug-In\Images\PlanitaxHorizonsIcon.png";
try
{
btnImg = System.Drawing.Image.FromFile(path,false);
btnImg = new Bitmap(btnImg,150,50);
}
catch
{
UI.FrmErrorInfo frmErrorInfo = new
PlanitaxWord.UI.FrmErrorInfo("Some of the files or folders are deleted or
moved from their location. Please Re Install the package.");
frmErrorInfo.ShowDialog(obj);
return;
}
object omissing = System.Reflection.Missing.Value;
if(objButton == null)
{
objButton = (CommandBarButton) objCommandBar.Controls.Add(1,
omissing , omissing , omissing , true);

objButton.Caption = "Planitax Horizons";
objButton.TooltipText = "Save to Planitax Horizons";
objButton.Style = MsoButtonStyle.msoButtonIcon;
Clipboard.SetDataObject(btnImg,false);
objButton.PasteFace();
}
}
//The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
objButton.Tag = "Planitax Horizons";
// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
objButton.OnAction = "!<PlanitaxWord.Connect>";
objButton.Visible = true;
objButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(this.objButton_Click);
objName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
objCommandBar = null;
objCommandBars = null;

Initialized = true;
wdApp.CustomizationContext = currentTpl;
currentTpl.Saved = true;
break;
}
}
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}

Please specify your comments on this.
 
S

Sandesh Patnam

Pavan,

Thanks for all the help provided. Now I am not getting the template save
alert.

Cindy,

I will implement the changes suggested. Thanks for extending the help.
 
S

Sandesh Patnam

Pavan & Cindy,

The purpose of our discussion was really helpful. I was able to remove the
button from Word successfully after uninstalling the plugin. Today I was
trying to implement the changes that Cindy suggested me. At that momment I
found a major problem because of using a custom template in Word. Let me
explain...

I am actually developing the plugin for both Word and Outlook applications.
The functionality of both the plugins are different from each other. So when
I opened the compose window of the Outlook application, it was reflecting
with the Word plugin functionality as Word is the default editor in Outlook.
To get rid of this, I am comparing the button with certain values to check if
the button contains the Word's click event or Outlook's click event within
the Outlook Connet.cs class. If the button is from Word, I am actually
overwriting the click event handler for Outlook.

Now in our present senario when I am using a custom template for word, I
opened the Outlook's Compose window. It initially loads Word's button and
then overwrites the event handler of the button with the outlook event
handler. When I closed the Outlook app, the process remains alive in the task
bar(As if its the same problem that we faced in Outlook 2000 object model).
Due to this the Word's button is not working properly. It works only when I
reset the toolbar manually in Word. I found that I am facing the problem only
when I use the custom word template and the process works fine when I use
Normal.dot.


Is there any possible way to reset the tool bar programmatically? If not
please suggest me an another alternative.
 
K

Ken Slovak - [MVP - Outlook]

The WindowActivate event can be used to check Window.EnvelopeVisible. If
True it's a WordMail item, if not it's a document item. Send to Recipient
can be handled both ways but I usually choose to just regard it as a mail
item.
 
P

pavan

Sandesh,
Assuming that the buttons are same and only functionality differs, try
Ken's appraoch in your button click event handlers. Do the check he
mentions and you can write the functionality depending on the result.
Like
if (Window.EnvelopeVisible == true){Outlook related functionality....}
else if (Window.EnvelopeVisible == false){Word related
functionality....}

Hope this helps
 
P

pavvu_kk

Sandesh,
Assuming that the buttons are same and only functionality differs, try
Ken's appraoch in your button click event handlers. Do the check he
mentions and you can write the functionality depending on the result.
Like
if (Window.EnvelopeVisible == true){Outlook related functionality....}
else if (Window.EnvelopeVisible == false){Word related
functionality....}

Hope this helps
 
K

Ken Slovak - [MVP - Outlook]

If the buttons are different you can hide or show the appropriate set as
needed.
 
S

Sandesh Patnam

Ken & Pavan,

Thanks for the reply.

I am using the similar approach to check the WordMail. Only thing is tha I
am checking the OnAction property of the button. Further when I open the Word
application, I am adding the Word functionality. This procedure works fine in
case of Normal.dot template. But when I use a custom template, the same
procedure doesnt seems to work.

Is there anything that I am missing while using the custom template?

Otherwise is there a way to use the same Normal.dot template and delete the
button while unistalling the plugin?

Thanks
 
P

pavan

Hey Sandesh,
I'm actually not able to understand as to how you are using OnAction
property to suit your requirement. OnAction is set to the prod id of
the add-in. Wouldn't it be same irrespective of who loads it as it is a
add-in property and not actually something related to the host
application. Just a doubt!

Anyways, why don't you try moving your check into the button click
event handler and see if it works for you?
Otherwise is there a way to use the same Normal.dot template and delete the
button while unistalling the plugin?
To my knowledge, atleast in Word, there is no other way out,
unfortunately. But I feel this a pretty clean way of adding custom
toolbars and menus unless we have issues :)
 
S

Sandesh Patnam

Pavan,

Yes. The prod id is same for the plugin irrespective of the host application
that launches it when the plugin is developed under same project for both the
host applications. But my requirement demands two different applications for
the two host application plugins. So I use two prod ids, two namespaces, two
connect.cs files within those two namespaces and so on. Thats the reason I am
able to differentiate the two plugins based on OnAction property.

I cannot check the button click event handler as it throws exception if the
button object is unassigned when Word opens adds the plugin for the first
time.
 
P

pavan

Sandesh,
I think your requirements are not very clear for me! If you are
developing two seperate add-ins, one for Outlook and one for Word then
1) When you are working with Outlook both the add-ins are getting
loaded. Is this right?
2) If yes, is the behavior that you desire or you want only the
Outlook add-in to load and not the Word add-in?
Pls let me know the answers so that I can help faster. Do give me any
info that I'm missing
 
S

Sandesh Patnam

Pavan,

My requirement is exactly the same as you mentioned in (2). I dont require
both the buttons to be loaded in Outlook.
 
K

Ken Slovak - [MVP - Outlook]

Where a custom button/toolbar is stored depends on the setting for
CustomizationContext. That can be Normal.dot, ActiveDocument, any loaded
template and probably other places. I never mess with the setting, I just
add to wherever the setting currently points to.

I've found in Word that I have to re-instantiate each button I access when I
want to set/check properties, even in the Click event for the button. For
example, changing the icon for a button in Click to set a different icon
depending on whether the button is depressed or not. That errors out unless
I re-instantiate the button.

I create the buttons and delete them on close of the document or window.
That way no artifacts are left over with orphan buttons.
 
P

pavan

But Ken this doesn't happen with Word! We can't just delete the
buttons. I've have faced this issue and found that word doesn't respect
the temporary attribute that we can set while adding the
toolbar/button. Infact, if it did then we would't even have to try
deleting it. And unfortunately delete also doesn't work. :) This is
best of to my knowledge. Pls let me know if there are any
workarounds/solutions for the same. I didn't get any from MSDN forums
and everyone suggested to use the custom template approach.

Sandesh,
You can try this.
1) In each of your event handlers (not button click handlers) like
OnConnection, NewDocument, DocumentOpen and so forth do the check which
Ken mentions.
2) If you find that it's Outlook that has instantiated Word then DO
NOT add the custom template. Otherwise load the Word custom template.
You can use wordApplication.Addins.Add method for the same
3) If you are placing the template in Startup folder don't do that.
Place it in a custom folder.
You might have an issue when you try to open word from Outlook and
independently. In such a scenario you might have to load both the
templates. And when you add a template in one document in Word it gets
added to all the documets. So you will have to delete the corresponding
template. You can use Activate and Deactivate events for the same.
OR
In Activate and Deactivete events do the check and try hiding the
correspoding toolbar which you do not require.

Pls feel free to post if you require more info!
 
K

Ken Slovak - [MVP - Outlook]

Of course you can delete them.

The Word document has to still be in scope of course. It goes with what I
was saying earlier in the thread, for every button access re-instantiate. I
re-instantiate the buttons in my cleanup code using the unique Tag
properties I've assigned and then delete each button I get in the cleanup
procedure.
 

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