【问题标题】:Run a makkro from an temporary Excel file从临时 Excel 文件运行 makkro
【发布时间】:2021-07-23 13:20:33
【问题描述】:

我正在编写一个程序,该程序从资源文件夹加载一个 excel,运行一个 makkro 并将 makkro 的输出保存到一个变量中。但是每次我尝试运行我的 makkro 时,程序都会告诉我它找不到我的 excel 所在的文件。现在我想知道我的程序是否能够到达临时文件路径,或者是否发生了与此不同的任何事情。

我认为这里的问题可能是复制 excel 会停用 makkros,我应该在复制后再次激活它们。如果这是一个现实问题,有人可以向我解释我如何才能超越这个问题吗?

编辑

代码似乎也能够读取临时文件。至少它找到了excel表。现在唯一仍然存在的问题是 makkros。

    public void CreateCopyOfResourcesExcel()
    {
        //temporary filepath to read the excel
        tempPath = System.IO.Path.GetTempFileName();

        //copy excel to this filepath
        System.IO.File.WriteAllBytes(tempPath, Properties.Resources.Vorlage_Sensoren);
        excelApp = new _Excel.Application();
    }



     private void button_WFx_Click(object sender, EventArgs e)
     {
        excelWorkbook = excelApp.Workbooks.Open(tempPath);
        excelSheet = excelWorkbook.Sheets["Tabelle2"];
        excelApp.Visible = false;

        //run measurement
        excelApp.Run("Vorlage_Sensoren.xlsm!readValue"); //<-- here comes the error (System.Runtime.InteropServices.COMException)

        RunSensorExcel(); //this is the function where I wanted to save the values
        
        //close excel
        excelWorkbook.Close(false);
        excelApp.Quit();

        Marshal.ReleaseComObject(excelWorkbook);
        Marshal.ReleaseComObject(excelApp);
      }  

【问题讨论】:

    标签: c# excel excel-interop temporary-files comexception


    【解决方案1】:

    解决方案

    这里的问题是我试图用 Excel 的旧名称运行 makkro。但是通过创建一个临时的,excel的名称发生了变化,程序无法识别旧的。

    所以我用以下代码修复了程序:

            //get makkro name with the new temp name of the excel
            string makkroName = Path.GetFileName(tempPath) + "!readValue";
            excelApp.Run(makkroName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多