Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening Excel in Visual C#

Posted on 2009-05-13
2
Medium Priority
?
851 Views
Last Modified: 2013-12-17
I am seting up a C# program from scatch and have aded in the Microsoft Excel 11.0 Object Libary.

I can reference this with the code:
   using Microsoft.Office.Interop.Excel;

but I can't use:
  using Excel;

It comes up with "The type or namespace name 'Excel' could not be found (are you missing a using directive or an assembly reference?)"

Is there soemthing obvious I'm missing?

Thanks
0
Comment
Question by:Enzar
2 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 2000 total points
ID: 24381265
When you specify the "using" directives at the top of your file, you're basically saying "If you happen to see an unqualified type, and its nested in the following namespace, just qualify the namespace for me because the names won't clash". The "." character is used either as just a character in the namespace's name, or to represent that a namespace is nested within another. For example, we could assume that the excel COM library's layout would look something like the following:

namespace Microsoft
{
     namespace Office
     {
          namespace Interop
          {
               namespace Excel
               {
                    // Excel COM library implementation
               }
          }
     }
}

When you compile your program, the compiler actually has no idea what "string" would be. That type doesn't exist (it's actually just an alias for the String class in the System namespace). Also, even though we may put

using System;

class MainMethod
{
     private static void Main()
     {
          // Input prompt, yada yada
          String msg = Console.ReadLine();
          // Processing
          Console.WriteLine(msg);
     }
}

the compiler has no idea what "String" is, or what "Console" is. Well, if "String" isn't the true name for the String class, then what is? Answer: System.String. A "fully-qualified" type-name consists of the namespace structure (such as Microsoft.Office.Interop.Excel.Workbook, vs just Workbook with a using Microsoft.Office.Interop.Excel at the top). Types must either be fully-qualified, or not qualified at all with a "using" statement at the top. In the case of the using statement, the compiler will actually qualify the name for you, so you don't have to! So, what's the point of namespaces then? Why not just use a "using" statement for everything? The answer is: because of type name clashing. For example, there are actually 3 different "Timers" in the .Net Framework (that I know of, at least), each within a different namespace: System.Threading, System.Windows.Forms, and System.Timers. Each has its own unique implementation, so if you create a new instance of one, and you have a using statement of two or more of the clashing Timers, how does the compiler know which object to utilize and instantiate? It doesn't, which is we we have namespaces.

Anyways, onto your problem. The general rule is: when specifying namespaces, namespaces must ALWAYS be fully-qualified. There's no namespace called "Excel" in any of the libraries you've referenced. Instead, the namespace you're *probably* looking for is Microsoft.Office.Interop.Excel, which you have a using statement for right near the top of your post. So, solution? Just remove the "using Excel;" part; the "Excel" namespace doesn't exist. It's "nested" within the other three ("Microsoft", "Office", and "Interop").

Hope that helps,
Nate
0
 

Author Comment

by:Enzar
ID: 24382302
thanks for this.

I'm hacing trouble acces worksheet.range object. I can use the get_range but range isn't an option.

Any ideas, thanks.
            Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
            excelApp.Visible = true;
            Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\Test.xls", 0, true, 2, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
            Sheets excelSheets = excelWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);
 
            Microsoft.Office.Interop.Excel.OLEObject bButton = (OLEObject)excelWorksheet.OLEObjects("OptionButton1");
            string str1 = bButton.LinkedCell();
            //
            // At this point excelWorksheet.range does not exist, .get_raneg does though.

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question