none
A little help: developing my first add-in for Word RRS feed

  • Question

  • I'm trying my hand at developing a Word Add-In and I have some experience using Visual Studio 2010 and C#. I tried following this Walkthrough but I got the following errors:

    ------ Rebuild All started: Project: WordAddIn1, Configuration: Debug Any CPU ------
    H:\src\make-word-doc\WordAddIn1\ThisAddIn.Designer.cs(18,68): error CS0234: The type or namespace name 'AddInBase' does not exist in the namespace 'Microsoft.Office.Tools' (are you missing an assembly reference?)
    H:\src\make-word-doc\WordAddIn1\ThisAddIn.Designer.cs(230,88): error CS0234: The type or namespace name 'RibbonCollectionBase' does not exist in the namespace 'Microsoft.Office.Tools.Ribbon' (are you missing an assembly reference?)

    I tried what I tried in the past, going through the Add Reference dialog and adding everything I think might help, which didn't help. Any pointers anyone can offer would be appreciated, as I often end up stuck and then either spend a great deal of time trying to figure it out or give up, but I'm sure there is an easier way.

    Here is the full module of code:

    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 WordAddIn1
    {
        public partial class ThisAddIn
        {
            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_Startup(object sender, System.EventArgs e)
            {
                this.Application.DocumentBeforeSave +=
                    new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
            }
            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
        }
    }

    Thursday, November 8, 2012 6:34 PM

All replies

  • Hi Kevin

    Which version of Office is installed on your development machine? And which version of the .NET Framework was selected when you created the add-in?

    I get the feeling that the project is set to use the .Netframework 3.5 (Office 2007) and the add-in has generated code for 4.0...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 9, 2012 9:33 AM
    Moderator
  • Hi Cindy,

    Thanks for the reply. In VS the project setting is for a target framework of .NET 4 (Client Profile). I have Office 2010 installed.

    I changed the target framework to .NET 3.5 and it built correctly and it runs! So, what does that mean?

    Friday, November 9, 2012 3:37 PM
  • Hi Kevin

    It means my brain reversed the situations (3.5 <-> 4.0).

    Usually, when I create a project using 2010 I get the code for .NET 4.0 and I wanted the code for 3.5, so I'm usually having to comment out certain lines of code (involving the same objects you saw, but different code) that are specific to 4.0. This is the first time I've seen that VSTO 2010 is doing the reverse - writing 3.5 code in a 4.0 project.

    FWIW, I just followed the steps on the page to which you link and I don't see the problem (using VS 2010 and Word 2010). Does the same thing happen if you try creating a new add-in?

    I'm going to move this discussion to the VSTO forum, as this issue certainly has more to do with the VSTO technology than the Word application.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, November 10, 2012 8:35 AM
    Moderator
  • Cindy,

    Thanks for your reply and for moving this topic to an apt forum.

    I think I need a bit more education. .NET versions are generally backward compatible, aren't they? I can run .NET 3.0 program when I have 4.0 installed, for example.

    Is it the case that If Office 2010 were built in .NET 3.5, and I generate a 4.0 add-in, it may include some 4.0 features that would cause it to break in Word 2010? However, should the source code fail to compile when I build for .NET 4.0? Put another way, is that Walkthrough source code only valid for .NET 3.5 (or lower) projects?

    Here is what I have - with the new project dialog set to .NET 3.5 (upper left pull-down), I can create a new project and insert the code from the Walkthrough and it works well. However, when I set the new project dialog to .NET 4.0 it fails. My machine is Win-7 and has .NET 4.0 installed.

    Monday, November 12, 2012 4:00 PM
  • Hi KevinBuchs,

    Thanks for posting in the MSDN Forum.

    I will involve some experts into your issue to see whether they can help you. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, November 14, 2012 7:08 AM
    Moderator
  • Hi,

    The project retargetting introduced the error message. The class 'RibbonCollectionBase' only exists in Office project that targets .NET Framework 4.0, so when you change target to .NET 3.5, it doesn't exists anymore.  See MSDN document of this class.

    AddinBase is a similar case.

    thanks.


    Forrest Guo | MSDN Community Support | Feedback to manager

    • Proposed as answer by Forrest GuoModerator Wednesday, November 14, 2012 9:04 AM
    • Unproposed as answer by KevinBuchs Wednesday, November 14, 2012 2:57 PM
    Wednesday, November 14, 2012 9:04 AM
    Moderator
  • Forest,

    Sorry, you misunderstood. The walkthrough code does NOT compile when the project is targeted to .NET 4.0, but it DOES compile when targeted to 3.5.

    If you can, please see my latest post before this one for the outstanding sub-questions that I have.

    - Kevin

    Wednesday, November 14, 2012 2:59 PM
  • Hi,

    My reply above explains possible cause for errors in your original post. It does conflict with fact that .NET 35 working, .NET 40 not working.

    Although the walkthrough doesn't mention which .NET Framework version as target framework, both 3.5 and 4.0 should work, and switching between them also works. That is called project retargeting.

    To diagnose the compilation problem in .NET 4.0 project, will you please share the project via Skydrive? (http://skydrive.com )


    Forrest Guo | MSDN Community Support | Feedback to manager

    Thursday, November 15, 2012 6:51 AM
    Moderator
  • Forrest,

    I don't think you have explained the error messages.

    I created the project in 4.0 and followed the walkthrough and the error messages were output as the only reasons for the failure of the build. That was in 4.0. If that project were making calls to functions only in 4.0, then it would have succeeded. When I did retarget to 3.5 or create the project in 3.5, the error messages were not present.

    If VS were generating code making 3.5 calls even though the project was targeted to 4.0, then there should have been no error messages because code making 3.5 calls should find those in 4.0.

    If the reverse were true, I was targeting 3.5 and getting those errors, then it might be a failure of VS to truly generate 3.5 code and instead generating code calling 4.0. However, this is not the situation in which error messages are generated.

    Here is the project. I appreciate any help and increased understanding you can lend me.

    Thursday, November 15, 2012 2:53 PM
  • Sorry for the delay response.

    The project targets .NET 35 and could not build. If I change the project target framework to 4.0 Client Profile, it works and text written to document before save.  Can you confirm?

    I suppose you want to target .NET 4.0, so we'd better stop trying to retarget .NET 3.5.

    ============Start discussion of .NET 3.5>>>>>>>>>>

    I noticed you have reference (MSORUNLIB.dll), it doesn't exists on my computer.  Can you let me know why you want that?

    There're multiple reference duplicate in the project:

    1. VBIDE
    2. Microsoft.Office.Interop.Word1

    Microsoft.Office.Tools.Common.v4.0.Utilities should be removed as you target .NET 35.


    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, November 20, 2012 3:37 AM
    Moderator
  • Forrest,

    You wrote:

    The project targets .NET 35 and could not build. If I change the project target framework to 4.0 Client Profile, it works and text written to document before save.  Can you confirm?

    The above is NOT the case - I cannot confirm it because something different is going on.

    • The walkthrough web page does not indicate that it targets 3.5 or 4.0.
    • When I create the VS project with the .NET 3.5 target, it builds correctly.
    • When I create the VS project with the .NET 4.0 target, it DOES NOT build correctly.
    • If I retarget the VS project, originally targeted at 4.0, and now targeted at 3.5 it builds correctly.

    This is what happens. This is what I CAN confirm. Please stop trying to fit my problem into the mold of a problem that you know how to solve and read carefully what I am writing.

    I suppose you want to target .NET 4.0, so we'd better stop trying to retarget .NET 3.5.

    I do not actually care whether it is targeted to 4.0 or 3.5, as I assume code targeted to 3.5 will run when I have 4.0 installed. What I do care about is understanding why I cannot target 4.0 with the Walkthrough.

    I noticed you have reference (MSORUNLIB.dll), it doesn't exists on my computer.  Can you let me know why you want that?

    These were added in just in an attempt to get it to build. I didn't know about them so I tried them. See my first post.

    There're multiple reference duplicate in the project:
    1. VBIDE
    2. Microsoft.Office.Interop.Word1

    Microsoft.Office.Tools.Common.v4.0.Utilities should be removed as you target .NET 35.

    This was just my attempt to add the references I found that might make it work, as in the first note. I have 4.0 installed.

    Thank you.

    Kevin


    • Edited by KevinBuchs Tuesday, November 20, 2012 3:32 PM
    Tuesday, November 20, 2012 3:29 PM