dipl0
dipl0

Reputation: 1077

The Type or Namespace Name Excel Could Not Be Found

So I am a new C sharp user. However on lines 4, 18, 19 and 20 I am recieving the following error:

The Type or Namespace Name Excel Could Not Be Found

I have imported the reference to the excel COM library, to no effect.

using System;
using System.IO;
using System.Diagnostics;
using Excel;
namespace CallPython
{

class Program
{

    private static bool ExcelCommentsChecker()
    {
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workBook = excelApp.Workbooks.Open(@"C:\Tools\comments.xlsx");
        foreach (var worksheet in workBook.Worksheets()
            if (worksheet.Comments != null)
            {
                Console.WriteLine("comments");
            }
            else
            {
                Console.WriteLine("The variable is set to false.");
            }
        //foreach (var row in worksheet.Rows)
        //   foreach (var cell in row.Cells)
        // https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-add-and-delete-worksheet-comments
        // https://www.c-sharpcorner.com/blogs/how-to-add-move-hide-remove-comments-to-an-excel-worksheet-cell-using-epplus-net-application-c-sharp-part-eight
        //https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of
        // if (cell != null) // Do something with the cells
        return false;
    }


    static void Main(string[] args)
    {
        // full path of python interpreter 
        string python = @"C:\Tools\python3\python.exe";

        // python app to call 
        string myPythonApp = @"C:\Tools\wordchecksinglefile.py";
        //https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of
        // Create new process start info 
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

        // make sure we can read the output from stdout 
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;

        // start python app with 3 arguments  
        // 1st arguments is pointer to itself,  
        // 2nd and 3rd are actual arguments we want to send 
        myProcessStartInfo.Arguments = myPythonApp;

        Process myProcess = new Process();
        // assign start information to the process 
        myProcess.StartInfo = myProcessStartInfo;

        // start the process 
        myProcess.Start();

        // Read the standard output of the app we called.  
        // in order to avoid deadlock we will read output first 
        // and then wait for process terminate: 
        StreamReader myStreamReader = myProcess.StandardOutput;
        string myString = myStreamReader.ReadLine();

        /*if you need to read multiple lines, you might use: 
            string myString = myStreamReader.ReadToEnd() */

        // wait exit signal from the app we called and then close it. 
        myProcess.WaitForExit();
        myProcess.Close();
        ///
        Console.WriteLine("Value received from script: " + myString);

        System.Threading.Thread.Sleep(3000);

        // write the output we got from python app 

    }
}

}

I know this is an import error but I am not quite advanced enough in C sharp to know where this error is occurring.

Upvotes: 0

Views: 12535

Answers (1)

Antoine V
Antoine V

Reputation: 7204

You're using Office Interop Objects but the used namespace isn't correct.

I think you want to use the word Excel in the code, so you must alias the namespace:

using Excel = Microsoft.Office.Interop.Excel;

then it should work

Excel.Application excelApp = new Excel.Application();

and make sure you have added the assembly Microsoft.Office.Interop.Excel into your project. How to add this assembly

Upvotes: 5

Related Questions