Posts mit dem Label Namespaces werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Namespaces werden angezeigt. Alle Posts anzeigen

Montag, 7. Januar 2013

Problem Adding Ribbon-Control to Office AddIn Projects

ThisRibbonCollection' does not contain a definition for 'GetRibbon'....

If you see this error, don't panic, the solution is rather simple:
To make matters easier for you, Office AddIns have a partial class "ThisRibbonCollection" which is automatically used to define your Ribbon Control as the MainRibbonControl of the application. This might seem, after the creation and a first run rather as magic, unless of course, you want to have all your application controls in a separate folder and therefore separate namespace from ThisAddIn.cs. (thx for the hint to http://qa.social.msdn.microsoft.com/Forums/en-SG/vsto/thread/3b117e7a-e3d5-4f10-8262-358b04494230)

However there is also a solution to this, allowing you to keep your RibbonControl in a separated folder and namespace:
  1. Open <RibbonControl>Designer.cs
  2. Go to the end of the file where you see partial class ThisRibbonCollection
  3. Create at the end of the file a new namespace section, with the root-namespace of your AddIn
  4. Move the class into the new namespace
  5. Since you are in a different namespace now, don't forget to add the necessary usings
Happy Coding...

Sonntag, 19. August 2012

Namespace Problems in C#

Hallo, just stumbled over two problems with namespaces in C#

First things first:

The type name 'IO' does not exist in the type 'Microsoft.Office.Interop.Word.System' when you have the following usings defined:
    using System.IO;
    using Microsoft.Office.Interop.Word;
The problem is that there exists a namespace Microsoft.Office.Interop.Word.System and Visual Studio or MSBuild somehow apply the System.*-namespaces (even they're defined before) to Micorosft.Office.Interop.Word and make it Microsoft.Office.Interop.Word.System.*

There is also a StackOverflow question about it, but unfortunately without any answer. My solution to this was more of a workaround, I named the namespace Microsoft.Office.Interop.Word.
    using System.IO;
    using word = Microsoft.Office.Interop.Word;
The downside of this workaround is that you need to use "word" in front of every type from the namespace. But at least the code compiles.

The second problem...

was a bit my fault but also not very clear in the beginning.
I have 2 Projects:
  • Common (for common functions, ExtensionMethods, ... which I may use also in the future)
  • Editor - the actual project
in Editor I had another folder "Common" which lead to the namespace Editor.Common

Now, the 1 Million $ question - how do you use in a class which has the namespace Editor.Common a class from the project and namespace Common? - The answer is simple: you can't.

And the moral of this story - don't mix your namespaces and give them names which are as unique as possible.


Happy Coding!
Peter