使用 VB6 从 .xls 文件中提取/显示宏 (VBA)(无需打开 Excel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/463973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract/Display Macro (VBA) from .xls files using VB6 (without opening Excel)
提问by
I understand it is possible to do so using Excel macro, see: Programmatically extract macro (VBA) code from Word 2007 docs, but what I want to do here is to use VB6 to make an application which does the same thing.
我知道可以使用 Excel 宏来执行此操作,请参阅:从 Word 2007 文档中以编程方式提取宏 (VBA) 代码,但我在这里想要做的是使用 VB6 制作一个执行相同操作的应用程序。
I am having problem on how to point to the Excel workbook (thisworkbook.VBproject
is used in the example above).
我在如何指向 Excel 工作簿(thisworkbook.VBproject
在上面的示例中使用)时遇到问题。
Is it possible to select any .xls file from the hd, say c:\try.xls
, and extract/show its macros? Please advise!
是否可以从高清中选择任何 .xls 文件,例如c:\try.xls
,并提取/显示其宏?请指教!
回答by onedaywhen
Set a reference to the Microsoft Excel 12.0 Object Library (or whatever version required) and use the workbook's VBProject.VBComponents collection e.g.
设置对 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
回答by onedaywhen
The following line:
以下行:
Set wb = Excel.Application.Workbooks.Open("C:\Book2.xls")
should be
应该
Set wb = app.Workbooks.Open("C:\Book2.xls")
回答by Chris Spicer
VbaDiffcan read Excel macros directly from the Excel file, exactly in the way you describe. If you need programmatic access, there is an Enterprise version with an API. You can do some quite fun stuff with this, like in these examples.
VbaDiff可以直接从 Excel 文件中读取 Excel 宏,完全按照您描述的方式。如果您需要以编程方式访问,可以使用带有 API 的企业版。你可以用它做一些非常有趣的事情,就像在这些例子中一样。
Full disclosure - I built this product. I kept coming across similar problems to what Dean was having and decided that the time had come to solve it once and for all!
完全披露 - 我构建了这个产品。我不断遇到与 Dean 遇到的类似问题,并决定是时候一劳永逸地解决它了!