Hi All,
I'm creating and installing a Word Add-in. The add-in creates a button on standard toolbar. When I uninstall the add-in the button is not getting removed. The add-in is the basic add-in from the link http://support.microsoft.com/default.aspx scid=kb;en-us;Q302901 as it is. The code in OnBeginShutdown is as follows:
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
MyButton = null;
}
Can anyone let me know what am I missing I've even tried using
oCommandBars["PayPalStdCommandButton"].Delete();
where oCommandBars is the command bars object.Regards,
Pavan

Button is not getting removed after uninstalling the Word add-in
Jermyh
And one thing I wanted to understand about this whole approach is whenever a word document is opened it uses the normal.dot as the template. Pls correct me if my understanding is wrong. So, if we are creating a new .dot file and adding the button to it, the document that is opened should use this new .dot file to reflect the button. how does this happen
TIA
Niranjan Arikela
Hi Sven,
Thanks for the help that you have given. Now I've a diferrent task. I've to add a table at the end of the document. I'm using this code to do that
object
start = 0;object end = 0;
WORD.Range tableLocation = activeDocument.Range(ref start, ref end);activeDocument.Tables.Add(tableLocation, 1, 1,
ref missing, ref missing);The table is getting added if I open a new document. But the problem that i'm facing here is that when I open an existing document it is not happening. Im presuming that it is coz Im saying start = 0 and end = 0. But if i replace 0s with something else like 10, it is not working for a new document also.
I hope you can help on this. I've tried searching on how to add a table at the end of the document. i got this code:
activeDocument.Characters[activeDocument.Characters.Count - 1].get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage);
activeDocument.Characters[activeDocument.Characters.Count - 1].get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage)
to get to the last character and place the image there. But this alsi doesnt seem to work.
Regards n TIA
Pavan
SergyR
The error 0x800A01A8 translates to 'Object Required'. The custom menu items are effectively being stored in the template, which is allready closed when the OnBeginShutdown method is called.
To work with another template, you should take these steps:
Add an empty word template to the setup project and install it to the startup folder of office (usally C:\Program Files\Microsoft Office\OFFICE11\STARTUP). This will make sure that the template gets loaded for each document. There should be no code or anything in the template, it will just be used as a container for the custom menus.
Now, before you add the menu button, set the customization context of word to the installed template. Be aware that the template will only be available when a document is loaded, so you can't do it in the OnConnection method. You should add an eventhandler to the DocumentChange event of the Word application object & add the menu's there.
It will look something like this (I typed this more or less out of my head, so it might contain some typing error or small inconsitensies)
private bool Intialized = false;
private Microsoft.Office.Interop.Word.Applcation wdApp;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectmode, object addInInst, ref System.Array custom)
{
wdApp = (Microsoft.Office.Interop.Word.Applcation)application;
wdApp.DocumentChange+=new Microsoft.Office.Interop.Word.ApplcationEvents4_DocumentChangeEventHandler(WordApp_DocumentChange);
}
private void WordApp_DocumentChanged()
{
if(!Initialized)
{
Microsoft.Office.Interop.Word.Template currentTpl =
wdApp.CustomizationContext;
foreach(Microsoft.Office.Interop.Word.Template tpl in wdApp.Templates)
{
if(tpl.name=="templatename.dot") //the name of the template you installed
{
wdApp.CustomizationContext = tpl;
//create your button / menu items here
Initialized = true;
wdApp.CustomizationContext = currentTpl
break;
}
}
}
}
Danny P
pardon my ignorance but I didn't understand
"Add an empty word template to the setup project and install it to the startup folder of office (usally C:\Program Files\Microsoft Office\OFFICE11\STARTUP). This will make sure that the template gets loaded for each document. "
from you post. Do you want me to create a simple word document and add it in the folder that you have mentioned. Or is there a way to do it programatically. Pls let me know this asap for you coz it seems like i cant implement the method you have suggested without getting this clarified.
Also, I had to ask you one more question. The addin that i'm creating is adding a new toolbar button every time i open the word document. Can you pls let me know why such a thing is happening.
Thanks a lot for your help
I'll wait for your clarification
Regards,
Pavan
lloydcodrington
Where do you call the code from
You should call it both from the NewDocument event (for new documents) as from the DocumentOpen event (for existing documents).
Sven
Chris4578
Ninerh
The OnStartupComplet method will run when you add-in is done loading. Since word opens a new document when its start, the code there will add the table to the new document, but the code will not run when you choose 'File - Open' or when you choose 'File new'. It will only run when word starts up.
Even when you open an existing document on startup (e.g. by doubleclicking it), chances are that the OnStartupComplete method gets called before the document is actually opened.
Sven
david2112
Add a handler to NewDocument & DocumentOpen & call the code from there (same way as explained in the earlier post)
Microsoft.Office.Interop.Word.ApplicationEvents4_Event tempObject =
(Microsoft.Office.Interop.Word.ApplicationEvents4_Event)wdApp;
tempObject.NewDocument+=new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(wdApp_NewDocument);
wdApp.DocumentOpen+=new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(wdApp_DocumentOpen);
private void wdApp_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
{
//add table to document here
}
private void wdApp_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
{
//add table to document here
}
Giovana
More or less. You need an empty template that will be a placeholder for your menubuttons. Just open word & without doing anything, choose file - save as and select 'document template' from the type dropdown.
Now if you just put the file in the folder, it won't get removed when you uninstall your add-in. If you add the document to your setup project, it will get removed when you uninstall your add-in.
Sven
Zero_One
First of all, set a try-catch block around the code where you remove the menu item. Office interop tends to 'swallow' up all ComExceptions. (The add-in will just exit without any error displaying).
If I'm not mistaken, commandbar buttons in word are added to a template (if no customizationcontext is set, this will be the normal template). Uninstalling the appliction will therefor not remove the buttons (as the uninstall will not rollback any changes made to your normal.dot file).
You could add an empty template to the setup project & use that to create the commandbars button on. Uninstalling the app will then also install the template & all the buttons/menu items created by the template.
You will have to set the CustomizationContext in word to the template before you add the buttons to the menu.
Sven
aniskhalife
Hey Sven
I should have tried the try-catch option long back. It never occured to me. thanks for pointing me in that direction.
Now I've found that it's generating an exception. the message says "Exception from HRESULT: 0x800A01A8". Can you help me in understanding what exactly this exception is and what has to be done to resolve it. Meanwhile i'll try searching it on the net.
I also understood what you said about the setting of customizationcontext. It will really be gr8 if you can give sample code on this. But if i set the customizationcontext then will the toolbar button be created for all the word documents
Thanks
Pavan
James Curran
im writing the code in OnStartupComplete method
will that not be sufficient
M1ckx
1. To specify a different folder, goto 'File System' in your setup project, right click on 'File System on Target Machine' choose 'Add Special Folder' and select the 'Program File Folder'.
Once the program files folder is added, right click on this folder and choose 'Add' and select 'Folder'. Name the folder 'Microsoft Office'
Continue this until you have the complete structure you want.
2. When you start Microsoft Word, it will load indeed normal.dot. It will also load all templates that are in the 'startup' folder of office (c:\program files\...\startup). So if you place a template there, it will be loaded togheter with normal.dot & any template that it attached to the document.
When you make changes to things that are stored inside a template (like styles & commandbar items/button), these changes are made in the normal.dot file by default. By setting the CustomizationContext to another template, you specify that all changes should be made to the specified template instead of normal.dot.
In your case, the menu items will be added to the template that is installed through an installer. If you uninstall the product, the template will be removed allong with your menu items.
Sven
webservicesforum