打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何在 VB 中以编程方式创建并调用 Excel 宏
      Private Sub Command1_Click()       ' Start Excel       Dim xlapp As Object 'Excel.Application       Set xlapp = CreateObject("Excel.Application")       ' Make it visible...       xlapp.Visible = True       ' Add a new workbook       Dim xlbook As Object 'Excel.Workbook       Set xlbook = xlapp.Workbooks.Add       ' Add a module       Dim xlmodule As Object 'VBComponent       Set xlmodule = xlbook.VBProject.VBComponents.Add(1) 'vbext_ct_StdModule       ' Add a macro to the module...       Dim strCode As String       strCode = _          "sub MyMacro()" & vbCr & _          "   msgbox ""Inside generated macro!!!"" " & vbCr & _          "end sub"       xlmodule.CodeModule.AddFromString strCode       ' Run the new macro!       xlapp.Run "MyMacro"       ' ** Create a new toolbar with a button to fire macro...       ' Add a new toolbar...       Dim cbs As Object 'CommandBars       Dim cb As Object 'CommandBar       Set cbs = xlapp.CommandBars       Set cb = cbs.Add("MyCommandBar", 1, , True) '1=msoBarTop       cb.Visible = True       ' Make it visible & add a button...       Dim cbc As Object 'CommandBarControl       Set cbc = cb.Controls.Add(1) '1=msoControlButton       ' Assign our button to our macro       cbc.OnAction = "MyMacro"       ' Set text...       cbc.Caption = "Call MyMacro()"       ' Set Face image...       ' 51 = white hand       ' 25 = glasses       ' 34 = ink dipper       ' etc...       cbc.FaceId = 51       ' Pause so you can inspect results...       MsgBox "All done, click me to continue...", vbMsgBoxSetForeground       ' Remember to release module       Set xlmodule = Nothing       ' Clean up       xlbook.Saved = True       xlapp.Quit      End Sub						
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
从命令行或批处理文件运行Excel宏的方法?
Visual Basic调用Excel
vb 关闭excel
VB中CreateObject函数用法
记录集导出到Excel的函数,速度很快。
[VBA][基础入门] 第4讲 对象模型
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服