So I have the office library imported, I say using Microsoft.Office.Interop;
I am trying to create a method that checks to see if an Excel workbook is open. Havent made any progress yet, because of this error.
when I declare Excel.Application exApp;
it says the Type or Namespace name'excel' could not be found.
it says that I need to declare it saying Microsoft.Office.Interop.Excel.Application exApp;
I really don't think that I did anything wrong here... There is no error on my import..
using Microsoft.Office.Interop;
namespace Month_End_Closing
{
public partial class Step1Form : Form
{
public Step1Form()
{
InitializeComponent();
}
static bool IsOpened(string wbook)
{
bool isOpened = true;
Excel.Application exApp;
return isOpened;
}
}
}
using
directives don't work like that. You can't provide part of a namespace name in the using
directive, and then the rest elsewhere.
From section 9.4.2 of the C# 5 specification:
A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.
...
A using-namespace-directive imports the types contained in the given namespace, but specifically does not import nested namespaces.
For example, this is invalid:
using System;
...
Console.WriteLine(IO.File.Exists("foo"));
So you could write:
using Microsoft.Office.Interop.Excel;
and then:
Application exApp;
assuming that Application
is otherwise unambiguous. (It sounds like it's not in your case.) Or you could use an alias:
using Excel = Microsoft.Office.Interop.Excel;
and then:
Excel.Application exApp;
See more on this question at Stackoverflow