繁体   English   中英

在excel文件中添加一个宏

[英]Add a macro in excel file

我有以下宏

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#执行它?

任何帮助将是最受欢迎的。

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 Project对象模型的宏安全性和信任访问:

在此输入图像描述

Siddharth Rout从此MSDN问题中提取的解决方案

这应该工作:

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#");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM