我知道可以使用Excel宏执行此操作,请参阅:Programmatically extract macro (VBA) code from Word 2007 docs,但我想要做的是使用VB6创建一个执行相同操作的应用程序。
我遇到了如何指向Excel工作簿的问题(上面的示例中使用了thisworkbook.VBproject
)。
是否可以从高清中选择任何.xls文件,比如c:\try.xls
,并提取/显示其宏?请指教!
答案 0 :(得分:2)
设置对Microsoft Excel 12.0对象库(或任何需要的版本)的引用,并使用工作簿的VBProject.VBComponents集合,例如
Sub ExportCode()
Dim app As Excel.Application
Set app = New Excel.Application
Dim wb As Excel.Workbook
Set wb = Excel.Application.Workbooks.Open("C:\Book2.xls")
Dim strExt As String
Dim VBComp As Object
For Each VBComp In wb.VBProject.VBComponents
Select Case VBComp.Type
Case 2 ' Class module
strExt = ".cls"
Case 3 ' Form
strExt = ".frm"
Case 1 ' Standard module
strExt = ".bas"
Case 100 ' Document?
strExt = ".cls"
Case Else
Stop ' What else is there?
strExt = ".cls"
End Select
VBComp.Export "C:\" & VBComp.Name & strExt
Next
wb.Close False
app.Quit
End Sub
答案 1 :(得分:1)
以下一行:
Set wb = Excel.Application.Workbooks.Open("C:\Book2.xls")
应该是
Set wb = app.Workbooks.Open("C:\Book2.xls")
答案 2 :(得分:1)