如何在 VB 中以编程方式创建并调用 Excel 宏
来源:互联网 发布:电信机房有多少端口 编辑:程序博客网 时间:2024/06/05 04:56
1. 在 Visual Basic 中创建一个标准 EXE 项目。默认情况下会创建 Form1。 2. 单击“项目”菜单中的“引用”,然后选中“Microsoft Visual Basic for Applications Extensibility”。 3. 向 Form1 中添加一个 CommandButton。 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
' 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
- 如何在 VB 中以编程方式创建并调用 Excel 宏
- 如何在VB中打开一个Excel文件并调用Excel文件自身的VBA宏?
- C#中编程创建、调用Excel 宏
- C#中编程创建、调用Excel 宏
- C#中编程创建、调用Excel 宏
- [VB.NET]如何在vb。net中打开一个excel表 并获取数据阿
- 如何在Windows NT中以编程方式管理用户权限
- 如何以编程的方式创建控件
- 如何在Excel中使用VB宏连接SAP系统
- 如何在pb中创建COM组件,并在asp中调用并返回结果集?
- 如何在pb中创建COM组件,并在asp中调用并返回结果集
- 在 Excel 中以编程方式复制工作表会导致运行时错误 1004
- SAS如何在VB中调用?
- 在VB中如何调用压缩工具winrar
- 如何将vbs脚本用VB封装为DLL,并在vbs脚本中调用
- 在delphi中调用excel有四种方式
- tornado学习笔记(二):如何在tornado中以异步的方式调用同步函数
- Linux Unicode 编程---如何(在程序中)加入并使用 Unicode 以实现外语支持
- 深入分析MFC文档视图结构(项目实践)
- 网际快车FlashGet使用技巧荟萃
- 以下文件中的行尾不一致,要将行尾标准化吗? 解决方法
- 在Eclipse中利用XMLBuddy开发XML
- [国际法]第六章 国际海洋法
- 如何在 VB 中以编程方式创建并调用 Excel 宏
- BCB编写DLL终极手册
- 隐藏树节点
- [国际法]第七章 国际空间法
- RMI一步一步来
- [国际法]第八章 外交和领事关系法
- 访问接口
- [国际法]第九章 条约法
- 实现接口