抱歉,我们找不到 Test.xlsm。它有可能被移动、重命名或删除吗?

Sorry, we couldn't find Test.xlsm. Is it possible it was moved, renamed or deleted?

我正在 运行从我的 C# 应用程序中获取一个宏代码。我已将 Test.xlsm 文件放在项目本身中,属性为:

构建操作:内容
复制到输出目录:始终复制。

但是,当我 运行 代码时,出现以下异常。

Sorry, we couldn't find Test.xlsm. Is it possible it was moved, renamed or deleted?

我无法提供硬编码路径,因为我必须创建该项目的 clickOnce 部署。

                // Define your Excel Objects 
            Excel.Application xlApp = new Excel.Application();

            Excel.Workbook xlWorkBook;

           //Start Excel and open the workbook.
            xlWorkBook = xlApp.Workbooks.Open("Test.xlsm");

            // Run the macros by supplying the necessary arguments
            xlApp.Run("Create", pFilePath1, pPath2);

            // Clean-up: Close the workbook
            xlWorkBook.Close(false);

            // Quit the Excel Application
            xlApp.Quit();

我找到了解决办法。我在文件名前加了Application.StartupPath:

string workbookPath = Application.StartupPath + @"\Test.xlsm";
//~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open(workbookPath);