Solved

Opening Excel in Visual C#

Posted on 2009-05-13
2
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 500 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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