How to read Excel Marco (.xlsm) file

c ahmad shukri Yong 26 Reputation points
2020-12-21T08:15:41.313+00:00

Hi all,

I have code like below to read Excel Macro (.xlsm) file;

Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.Workbook wk = objExcel.Workbooks.Open(@"'" + Path.GetDirectoryName(label1.Text) + "'", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            objExcel.Visible = true;
            Microsoft.Office.Interop.Excel.Worksheet ws = wk.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

But I got the exception below

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ExcelToPPT_DEV.exe

Additional information: Sorry, we couldn't find 'D:\New Dev Excel to PPT'.xlsx. Is it possible it was moved, renamed or deleted?

the filename is test.xlsm. but it couldn't find the file.

Thank you.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-12-22T01:54:41.577+00:00

    When I use a fixed path, there is no problem with the code.
    And according to the error message, the problem is on the wrong path.
    Path.GetDirectoryName Method is to get the folder where the file you specify is located.
    If label.Text is "d:\test\test.xlsm", then the result of this method will be "d:\test", you need to add a specific file name later.
    Also, please don't add single quotes at the beginning and end, it will produce an invalid path.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.