none
Custom Add-in functionality for Word 2010 not going away even after cleaning the solution. RRS feed

  • Question

  • I created the sample Add-in for Word 2010 described in this MSDN walkthrough. I followed the example word-by-word. The Add-in worked fine. But now even after removing the add-in, the Add-in functionality keeps working. That is, when I try to save a Word document, it adds the following text at the beginning of the document: “This text was added by using code”. This text is taken from the following code of the above MSDN link:

    void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
        Doc.Paragraphs[1].Range.InsertParagraphBefore();
        Doc.Paragraphs[1].Range.Text = "This text was added by using code.";
    }

    I remove the add-in by using “Clean Solution” option from my VSTO 2010 project, and I can see that after cleaning the solution the Add-in FirstWordAddIn has been removed from the list of Com Add-Ins in MS Word, and that the bin, bin\Debug, obj and obj\Debug  folders are completely empty as expected.

    I also noticed that if I rebuild the add-in after cleaning the solution, the Add-in re-appears in the list of Com Add-Ins and that all of the above mentioned folders get re-populated (as expected). But, when I clean the solution again, everything gets cleaned up (as expected); but then when I save a document, the above mentioned text gets added “twice” at the beginning of the document. This behavior keeps repeating if I keep cleaning the and keep rebuilding the solution – that is, if I do it third time, the above text gets added three times at the beginning of a document when I try to save it.

    One more observation: When I try to create a word document, the new document shows the following previously added text by the Add-in even though I have completely cleaned up the solution: "This text was added by using code." This text shows up as many times as rebuilt the solution. That is, if rebuilt the solution 3 times and cleaned it up even more than three times, when I open Word to create a new document, the above text would appear at the begining of the document repeated 3 times. However, if I open an existing Word document, this text does not appear.

    My code is exactly a copy from the above mentioned MSDN article. But I am copying here from my VSTO 2010 solution on a request by Quist Zhang: ThisAddIn.cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Word;
    
    namespace FirstWordAddIn
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
            }
    
            void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
    
                Doc.Paragraphs[1].Range.InsertParagraphBefore();
                Doc.Paragraphs[1].Range.Text = "This text was added by using code.";
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }
    

    This is very annoying, please help.
    Thanks..Nam


    • Edited by namwam Thursday, September 27, 2012 5:17 PM added more content on a reader's request.
    Tuesday, September 25, 2012 8:15 PM

All replies

  • Hi .Nam

    Try shutting down Word, shutting down Visual Studio and rebooting Windows.

    It sounds to me as if something is "hanging" in memory. If an event is getting triggered multiple times it means multiple delegates have been instantiated...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 26, 2012 8:32 AM
    Moderator
  • Hi Nam,

    Thank you for posting in the MSDN Forum.

    It will be much helpful for troubleshooting if you could provide your full code or upload your project via SkyDrive and then share it.

    I look forward to your reply.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 27, 2012 8:56 AM
    Moderator
  • Cindy, thanks for the response. Although I had already tried your suggestion, I tried it again - but still the same problem.

    I have added two more paragraphs at the end of my original post. That may shed more light on the issue. You may want to the read the paragraph starting with "One more observation".

    Nam

    Thursday, September 27, 2012 5:21 PM
  • Quist,

    Thanks for trying to help. The code is exact copy from the MSDN walkthrough . But I have copied it to my original post per your request.

    Thanks..Nam

    Thursday, September 27, 2012 5:25 PM
  • Hi Nam

    <<When I try to create a word document, the new document shows the following previously added text by the Add-in even though I have completely cleaned up the solution>>

    This indicates that something is writing text to the Normal.dotm template, not something that would happen with Word in its default installation configuration...

    Clean your solution and don't rebuild - in other words, it shouldn't be running when you start Word.

    Start Word. You see multiple instances of this text in the new document?

    Look at the Word application's Title Bar. You should see the document name there. What is it?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, September 29, 2012 6:01 AM
    Moderator
  • Cindy, thanks for trying to help. An extra  instance of this text appears only when I rebuild the solution. I cleaned the solution, verified that the bin and obj folders and their subfolders are completely empty, started the Word, the Word application’s title bar shows the default name “Document1” as expected. The previously added repeated text is still there.

    -Nam

    Tuesday, October 2, 2012 12:09 AM
  • Hi Nam

    OK, so far, so good...

    You should be able to get rid of the previously added text by opening the Normal.dotm template and deleting it.

    This isn't always easy to find, so the quickest way is:

    1. Alt+F11 (open the VBA editor)
    2. Ctrl+G (open the Immediate Window)
    3. Type: NormalTemplate.OpenAsDocument
    4. Press ENTER

    That should open Normal.dotm so that you can make the deletion.

    Of bigger concern to me is why this text is being written to Normal.dotm in the first place. That shouldn't be happening...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 2, 2012 5:42 AM
    Moderator
  • Cindy,

    Your suggestion worked but the exact same problem resurfaced after I rebuilding the solution. As before, cleaning the solutiion doesn't help either.

    The system configuration are as follows:
    Windows Vista Business – Service Pack 2
    MS Office Professional Plus 2010 Version 14.0.6123.5001 (32 bit)
    System up-to-date with Microsoft last updates installed on: 9/16/2012

    Thanks..Nam

    Wednesday, October 3, 2012 3:49 PM