【问题标题】:Add a macro in excel file在excel文件中添加宏
【发布时间】:2015-09-10 20:55:20
【问题描述】:

我有以下宏

Sub test()
    Dim xsheet As Worksheet
    For Each xsheet In ThisWorkbook.Worksheets
        xsheet.Select
        With xsheet.UsedRange
            .Value = .Value
        End With
    Next xsheet      
End Sub

有没有办法将它添加到 excel 文件并使用 c# 执行它?

任何帮助将不胜感激。

【问题讨论】:

标签: c# excel vba


【解决方案1】:

1) 这是我使用 .Net 参考适用于 Excel 的代码: Microsoft.Office.Interop.Excel v14(不是 ActiveX COM 参考):

using System;
using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
    RunVBATest();
}

public static void RunVBATest()
{
    Application oExcel = new Application();
    oExcel.Visible = true;
    Workbooks oBooks = oExcel.Workbooks;
    _Workbook oBook = oBooks.Open("C:\\temp\\Book1.xlsm");

    // Run the macro.
    RunMacro(oExcel, new Object[] { "TestMsg" });
    //Run a macro with parameters        
    //RunMacro(oExcel, new Object[] { "ShowMsg", "Hello from C# Client", "Demo to run Excel macros from C#" });

    // Quit Excel and clean up
    oBook.Saved = true;
    oBook.Close(false);
    oExcel.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
}

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

}
}
}
}

2) 确保将宏代码放入模块(全局 BAS 文件)中..

Public Sub TestMsg()

MsgBox ("Hello Stackoverflow")

End Sub

3) 确保启用对 VBA 项目对象模型的宏安全和信任访问:

【讨论】:

    【解决方案2】:

    Siddharth Rout 的解决方案从这个MSDN issue 中提取

    这应该可行:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //~~> Define your Excel Objects
                Excel.Application xlApp = new Excel.Application();
    
                Excel.Workbook xlWorkBook;
    
                //~~> Start Excel and open the workbook.
                xlWorkBook = xlApp.Workbooks.Open("E:\\Users\\Siddharth Rout\\Desktop\\book1.xlsm");
    
                //~~> Run the macros by supplying the necessary arguments
                xlApp.Run("test");
    
                //~~> Clean-up: Close the workbook
                xlWorkBook.Close(false);
    
                //~~> Quit the Excel Application
                xlApp.Quit();
    
                //~~> Clean Up
                releaseObject(xlApp);
                releaseObject(xlWorkBook);
            }
    
            //~~> Release the objects
            private void releaseObject(object obj)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                catch (Exception ex)
                {
                    obj = null;
                }
                finally
                {
                    GC.Collect();
                }
            }
        }
    }
    

    如果你的宏有参数,让我们说:

    Sub ShowMsg(msg As String, title As String)
        MsgBox msg, vbInformation, title
    End Sub
    

    您必须将xlApp.Run("test"); 更改为xlApp.Run("ShowMsg", "Hello from C# Client", "Demo to run Excel macros from C#");

    【讨论】:

    • 哈哈..我忘记了那个帖子:D
    • 好吧,既然你在这里,你应该回答并获得赏金:-p
    • 不。这都是你的:)
    • @SiddharthRout 如何在 excel 文件中插入宏?
    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多