Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. The type or namespace name 'Excel' could not be found

The type or namespace name 'Excel' could not be found

Scheduled Pinned Locked Moved C#
comcsharpdotnethelpquestion
18 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    pmcm wrote:

    var x1 = new Excel.Application();

    try: var x1 = new Microsoft.Office.Interop.Excel.Application();

    Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns

    P Offline
    P Offline
    pmcm
    wrote on last edited by
    #3

    your suggestion worked but instead of doing that every time throughout my code I've set this up:

    using Excel = Microsoft.Office.Interop.Excel;

    any idea why my project doesn't seem to be picking up the references that I added? Thanks

    L L 2 Replies Last reply
    0
    • P pmcm

      your suggestion worked but instead of doing that every time throughout my code I've set this up:

      using Excel = Microsoft.Office.Interop.Excel;

      any idea why my project doesn't seem to be picking up the references that I added? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      This is the correct way to do it, as described here[^].

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      L 1 Reply Last reply
      0
      • P pmcm

        your suggestion worked but instead of doing that every time throughout my code I've set this up:

        using Excel = Microsoft.Office.Interop.Excel;

        any idea why my project doesn't seem to be picking up the references that I added? Thanks

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #5

        when you do

        using some1.some2.some3;

        then the C# compiler will add "some1.some2.some3" to the list of known prefixes, which it uses to locate a type. Therefore,

        using System.Windows.Forms;
        ...
        Form f=new Form(); // works as the full type name is System.Windows.Forms.Form
        Forms.Form f2=f; // doesn't work as now prefixing System.Windows.Forms would result in a double "Forms"

        :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum


        Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

        1 Reply Last reply
        0
        • L Lost User

          This is the correct way to do it, as described here[^].

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          That doesn't work in .NET 2.0+, since you can't prefix a class name with a "part" of the namespace like that.

          Bastard Programmer from Hell :suss:

          L 1 Reply Last reply
          0
          • P pmcm

            Hi everyone, I'm working in VS2010 and trying to create an app that interacts with Excel. I've added The MS Excel 14.0 Object Library reference (COM Tab) and at the top of my code I have the following:

            using Microsoft.Office.Interop;
            using Microsoft.Office.Interop.Excel;
            using Microsoft.Office.Interop.Word;

            I've tried to do:

            var x1 = new Excel.Application();

            and I get the error. My project is .Net framework 4. I've tried to find an answer on Google and did what was suggested on a few other forums but still this is not working for me anyone any ideas why?

            D Offline
            D Offline
            Dinesh92
            wrote on last edited by
            #7

            i think you should use "Add reference", right click on form->Add reference, there you will get all the namespace which are available. :)

            P 1 Reply Last reply
            0
            • L Lost User

              That doesn't work in .NET 2.0+, since you can't prefix a class name with a "part" of the namespace like that.

              Bastard Programmer from Hell :suss:

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              OP is using .NET 4.0.

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              L 1 Reply Last reply
              0
              • D Dinesh92

                i think you should use "Add reference", right click on form->Add reference, there you will get all the namespace which are available. :)

                P Offline
                P Offline
                pmcm
                wrote on last edited by
                #9

                I've added the MS Excel 14.0 Object Library reference (COM Tab), Richard the link that you have recommended is one that I have already followed but I was still getting the error.

                L 1 Reply Last reply
                0
                • L Lost User

                  OP is using .NET 4.0.

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Shouldn't work in 4.0 either, but I'm too lazy to give it a try right now :)

                  Bastard Programmer from Hell :suss:

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Shouldn't work in 4.0 either, but I'm too lazy to give it a try right now :)

                    Bastard Programmer from Hell :suss:

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Eddy Vluggen wrote:

                    Shouldn't work in 4.0 either

                    I've used it in 3.0 and it worked fine. Did you read the linked article I referred to?

                    Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Eddy Vluggen wrote:

                      Shouldn't work in 4.0 either

                      I've used it in 3.0 and it worked fine. Did you read the linked article I referred to?

                      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      That tells us that you have probably used VB to implement the example, as C# doesn't allow a prefix of the classname with a partial namespace;

                      Namespace Mine.Test
                      Public Class SomeClass
                      Public Property P As Guid
                      End Class
                      End Namespace

                      --

                      Imports ScratchVb.Mine

                      Module Module1
                      Sub Main()
                      ' VB.NET allows you to use "a part" of the namespace as a prefix
                      Dim X As Object = New Test.SomeClass()
                      End Sub
                      End Module

                      using System;
                      namespace Mine.Test
                      {
                      class SomeClass
                      {
                      public Guid P { get; set; }
                      }
                      }

                      --

                      using Mine;

                      namespace Scratch
                      {
                      class Program
                      {
                      static void Main(string[] args)
                      {
                      // Prefixing a part of the name isn't allowed
                      Object X = new Test.SomeClass();
                      // Full namespace is allowed, of course;
                      Object X = new Mine.Test.SomeClass();
                      }
                      }
                      }

                      Yes, read the article some time ago. Did you try it? :)

                      Bastard Programmer from Hell :suss:

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        That tells us that you have probably used VB to implement the example, as C# doesn't allow a prefix of the classname with a partial namespace;

                        Namespace Mine.Test
                        Public Class SomeClass
                        Public Property P As Guid
                        End Class
                        End Namespace

                        --

                        Imports ScratchVb.Mine

                        Module Module1
                        Sub Main()
                        ' VB.NET allows you to use "a part" of the namespace as a prefix
                        Dim X As Object = New Test.SomeClass()
                        End Sub
                        End Module

                        using System;
                        namespace Mine.Test
                        {
                        class SomeClass
                        {
                        public Guid P { get; set; }
                        }
                        }

                        --

                        using Mine;

                        namespace Scratch
                        {
                        class Program
                        {
                        static void Main(string[] args)
                        {
                        // Prefixing a part of the name isn't allowed
                        Object X = new Test.SomeClass();
                        // Full namespace is allowed, of course;
                        Object X = new Mine.Test.SomeClass();
                        }
                        }
                        }

                        Yes, read the article some time ago. Did you try it? :)

                        Bastard Programmer from Hell :suss:

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        Eddy Vluggen wrote:

                        That tells us that you have probably used VB to implement the example, as C# doesn't allow a prefix of the classname with a partial namespace;

                        Wrong on both counts.

                        Eddy Vluggen wrote:

                        Yes, read the article some time ago. Did you try it?

                        Yes I tried it, using C# as i)I never use or have used VB/VB.NET and ii)the title of the article is How to automate Microsoft Excel from Microsoft Visual C#.NET!

                        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                        L 1 Reply Last reply
                        0
                        • P pmcm

                          I've added the MS Excel 14.0 Object Library reference (COM Tab), Richard the link that you have recommended is one that I have already followed but I was still getting the error.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          pmcm wrote:

                          Richard the link that you have recommended is one that I have already followed but I was still getting the error.

                          Can you show your code and the exact text of the error message?

                          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                          P 1 Reply Last reply
                          0
                          • L Lost User

                            Eddy Vluggen wrote:

                            That tells us that you have probably used VB to implement the example, as C# doesn't allow a prefix of the classname with a partial namespace;

                            Wrong on both counts.

                            Eddy Vluggen wrote:

                            Yes, read the article some time ago. Did you try it?

                            Yes I tried it, using C# as i)I never use or have used VB/VB.NET and ii)the title of the article is How to automate Microsoft Excel from Microsoft Visual C#.NET!

                            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #15

                            Richard MacCutchan wrote:

                            Yes I tried it, using C#

                            Never mind :)

                            Bastard Programmer from Hell :suss:

                            L 1 Reply Last reply
                            0
                            • L Lost User

                              Richard MacCutchan wrote:

                              Yes I tried it, using C#

                              Never mind :)

                              Bastard Programmer from Hell :suss:

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              Eddy Vluggen wrote:

                              Never mind

                              I don't mind, it's you that kept banging on about it, like a :mad:

                              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                              1 Reply Last reply
                              0
                              • L Lost User

                                pmcm wrote:

                                Richard the link that you have recommended is one that I have already followed but I was still getting the error.

                                Can you show your code and the exact text of the error message?

                                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                P Offline
                                P Offline
                                pmcm
                                wrote on last edited by
                                #17

                                I fixed this issue by doing:

                                using Microsoft.Office.Interop;
                                using Excel = Microsoft.Office.Interop.Excel;
                                using Word = Microsoft.Office.Interop.Word;

                                L 1 Reply Last reply
                                0
                                • P pmcm

                                  I fixed this issue by doing:

                                  using Microsoft.Office.Interop;
                                  using Excel = Microsoft.Office.Interop.Excel;
                                  using Word = Microsoft.Office.Interop.Word;

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #18

                                  That's what I suggested yesterday.

                                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups