10

我正在寻找运行一个宏,让我们在 WorkSheet02 上的 WorkSheet01 中将其称为 Macro01。

使用Microsoft.Office.Interop.Excel 命名空间,我打开了 WorkSheet01。

public void Main_CodedStep()
    {
        // Object for missing (or optional) arguments.
        object oMissing = System.Reflection.Missing.Value;

        // Create an instance of Microsoft Excel
        Excel.ApplicationClass oExcel = new Excel.ApplicationClass();

        // Make it visible
        oExcel.Visible = true;

        // Open Worksheet01.xlsm
        Excel.Workbooks oBooks = oExcel.Workbooks;
        Excel._Workbook oBook = null;
        oBook = oBooks.Open("C:\\Users\\Admin\\Documents\\Worksheet01.xlsm", oMissing, oMissing,
            oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, 
            oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
    }

然后我使用自动化脚本来提取报告。此报告是通过 IE 的下载提示而不是 Interop 打开的。

当我尝试通过 C# 运行宏时出现问题(我制作了另一个新的 Excel.ApplicationClass();只是这样编译,我相信这是我的失误之一。)

public void FirstMacro_CodedStep()
    {
        // Create an instance of Microsoft Excel
        Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
        Console.WriteLine("ApplicationClass: " + oExcel);

        // Run the macro, "First_Macro"
        RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"});

        //Garbage collection
        GC.Collect();
    }

    private void RunMacro(object oApp, object[] oRunArgs)
    {
        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
    }

当此方法运行时,它会在 Worksheet01 而不是 Worksheet02 上从 Worksheet01 运行宏。它还在我的文档中寻找工作表,所以我把它移过来看看会发生什么。

回顾:

  1. 打开工作表01
  2. 通过脚本从 MSIE 获取并打开报告 (Worksheet02)
  3. 在 Worksheet02 上从 Worksheet01 运行 Macro01

资源:

http://support.microsoft.com/kb/306683

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx

对于那些想尝试它的人,请将其添加到您的 using 指令中:

using System.Reflection;
using Microsoft.Office.Core; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "office"
using Excel = Microsoft.Office.Interop.Excel; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "Microsoft.Office.Interop.Excel"
4

3 回答 3

13

我找到了一个我想分享的解决方案。首先,我删除了打开 Worksheet01 的位。然后我让我的自动化脚本将 .CSV 保存到我的文档中。然后我使用我必须打开 Worksheet01 的代码来打开下载的文件。此时的关键是 Worksheet01 与 Worksheet02 位于 Documents 文件夹中。最后,我使用代码从 Worksheet01 运行宏,该宏在 Worksheet02 上运行。

    public void WebTest_CodedStep()
    {
        // Object for missing (or optional) arguments.
        object oMissing = System.Reflection.Missing.Value;

        // Create an instance of Microsoft Excel
        Excel.ApplicationClass oExcel = new Excel.ApplicationClass();

        // Make it visible
        oExcel.Visible = true;

        // Define Workbooks
        Excel.Workbooks oBooks = oExcel.Workbooks;
        Excel._Workbook oBook = null;

        // Get the file path
        string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        path = path + "\\Worksheet02.csv";

        //Open the file, using the 'path' variable
        oBook = oBooks.Open(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,  oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

        // Run the macro, "First_Macro"
        RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"});

        // Quit Excel and clean up.
        oBook.Close(false, oMissing, oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook);
        oBook = null;
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks);
        oBooks = null;
        oExcel.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel);
        oExcel = null;

        //Garbage collection
        GC.Collect();
    }

    private void RunMacro(object oApp, object[] oRunArgs)
    {
        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
    }
于 2013-01-10T19:50:19.877 回答
2

我运行这个 C# VSTO 代码来调用 VBA 宏,这是我使用的语法:

this.Application.Run("mymacro");

编辑:

宏是工作簿范围内的,也许您需要在运行宏之前将 Sheet2 设为活动工作表,例如:

foreach (Worksheet worksheet in workbook.Sheets.ComLinq<Worksheet>())
{
    if (worksheet.Name == "Sheet2") worksheet.Activate();
}
于 2013-01-10T00:29:19.517 回答
0
    public void ExecuteMacro()
    {
    //    using Excel = Microsoft.Office.Interop.Excel;
    //    using System;
    //    using System.IO;

        string path = Environment.CurrentDirectory;
        string filePath = "";
        string[] fileEntries = Directory.GetFiles(".\\Source");
        foreach (string fileName in fileEntries)
        {
            if (fileName.IndexOf(".xlsm") > 0 && fileName.IndexOf("$")<1) filePath = fileName;
        }

        if (filePath == "") return;

        filePath = filePath.Replace(".\\", "\\");
        string fileDest = filePath.Replace("Source","Processed");
        filePath = path+filePath;
        fileDest = path+fileDest;

        Excel.Application ExcelApp = new Excel.Application();
        Excel.Workbook wb = ExcelApp.Workbooks.Open(filePath, ReadOnly: false);

        try
        {
            ExcelApp.Visible = false;
            ExcelApp.Run("UpdateSheets");

            try
            {
                File.Delete(fileDest);
            }
            catch (Exception) { }

            wb.SaveAs(fileDest);
        }
        catch (Exception) { }

        wb.Close(false);
        ExcelApp.Application.Quit();
        ExcelApp.Quit();
    }
于 2021-09-30T04:25:09.310 回答