c# - 通过 C# : Run a macro from one workbook on another? 运行 Excel 宏

标签 c# excel vba office-automation

我想运行一个宏,我们将它称为 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. 打开 Worksheet01
  2. 通过脚本从 MSIE 获取并打开报告 (Worksheet02)
  3. 从 Worksheet01 到 Worksheet02 运行 Macro01

资源:

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

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

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

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"

最佳答案

我找到了一个我想分享的解决方案。 首先,我删除了打开 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);
    }

关于c# - 通过 C# : Run a macro from one workbook on another? 运行 Excel 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248592/

相关文章:

excel - 应用匹配功能如何复制粘贴数据

c# - 使用数据源的 NUnit 数据驱动单元测试

c# - 为什么 ASP.NET Identity 返回 401 而不是重定向到登录页面?

c# - SharpSVN - 服务器证书验证失败

excel - 将 Excel 图表导出到 SVG 创建一个空文件

Python Pandas read_excel 无法识别空单元格

excel - 使用系统时间每 30 秒保存一次 Excel 文件

c# - 相当于 Prism ViewModels 中的 WPF DependencyProperties

VBA 读取密码保护的宏

vba - Excel VBA - 在特定非默认 (A1) 位置创建表