代码之家  ›  专栏  ›  技术社区  ›  Gareth Hayter

从代码调用Excel宏记录器

  •  4
  • Gareth Hayter  · 技术社区  · 16 年前

    我需要一种方法在Excel中以编程方式启动宏记录器,并为将要创建的新宏提供名称。

    这可以来自VSTO或VBA,也可以使用Office Interop程序集。

    有什么办法可以做到吗?

    1 回复  |  直到 12 年前
        1
  •  2
  •   Lance Roberts    16 年前

    在VBA中:

    Dim ctrlStart As CommandBarControl, ctrlStop As CommandBarControl
    Set ctrlStart = Application.CommandBars.FindControl(ID:=184)
    Set ctrlStop = Application.CommandBars.FindControl(ID:=2186)
    
    ctrlStart.Execute
    
    'name part would go here, but first you have to deal with a modal dialog
    
    ctrlStop.Execute
    

    看起来recordmacro控件上的execute方法打开了一个模式对话框。无法向此提供参数,也无法执行sendKeys之类的操作。我看到的唯一方法是写一个子函数,它将在事实之后重命名宏。要确定新宏的名称有点复杂,您仍然需要处理一个对话框。

    推荐文章