我正在从我的 C# 应用程序运行宏代码。我已使用 Properties 将 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);
关于c# - 抱歉,我们找不到 Test.xlsm。是否有可能被移动、重命名或删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29203477/