使用VB6列出并运行Excel工作表macros

我正在尝试使用vb6访问存储在Excel工作簿中的macros。 我打开一个包含几张纸的工作簿。 其中一些有macros。 有什么办法可以列出所有的macros(名称),并给予用户cnahce从列表中选取一个或多个macros并运行它们?

同样,我希望能够列出button(放置在工作表上的命令button)而不是内置的工具栏button,并最终点击它,再次向用户提供button的名称(或最好的标题)。

提前感谢您的帮助!

下面是一些代码从一个Excel工作簿通过使用Visual Basic 6.0从工作表(如在示例中的c:\ abc.xls)中加载一个macros列表到一个名为List1的列表框中的macros如何从Excel工作簿中检索macros的名称 :

Private Sub LoadMacrosList() ' Declare variables to access the Excel workbook. Dim objXLApp As Excel.Application Dim objXLWorkbooks As Excel.Workbooks Dim objXLABC As Excel.Workbook ' Declare variables to access the macros in the workbook. Dim objProject As VBIDE.VBProject Dim objComponent As VBIDE.VBComponent Dim objCode As VBIDE.CodeModule ' Declare other miscellaneous variables. Dim iLine As Integer Dim sProcName As String Dim pk As vbext_ProcKind ' Open Excel, and open the workbook. Set objXLApp = New Excel.Application Set objXLWorkbooks = objXLApp.Workbooks Set objXLABC = objXLWorkbooks.Open("C:\ABC.XLS") ' Empty the list box. List1.Clear ' Get the project details in the workbook. Set objProject = objXLABC.VBProject ' Iterate through each component in the project. For Each objComponent In objProject.VBComponents ' Find the code module for the project. Set objCode = objComponent.CodeModule ' Scan through the code module, looking for procedures. iLine = 1 Do While iLine < objCode.CountOfLines sProcName = objCode.ProcOfLine(iLine, pk) If sProcName <> "" Then ' Found a procedure. Display its details, and then skip ' to the end of the procedure. List1.AddItem objComponent.Name & vbTab & sProcName iLine = iLine + objCode.ProcCountLines(sProcName, pk) Else ' This line has no procedure, so go to the next line. iLine = iLine + 1 End If Loop Set objCode = Nothing Set objComponent = Nothing Next Set objProject = Nothing ' Clean up and exit. objXLABC.Close objXLApp.Quit End Sub 

这里有一些代码来运行一个macros( 源 ):

 Private Sub RunMacro(string macroName) 'create and object (Excel SpreadSheet) Dim oXL As Object Set oXL = CreateObject("Excel.Application") ' Open the workbook that contains the macro to run. oXL.Workbooks.open "C:\ABC.XLS" ' 'as object opens invisible, make visible if needed, if not omit 'the line oXL.visible=true ' oXL.Visible = True ' ' Run the macro. oXL.Application.Run "abc.xls!" & macroName ' ' Quit Microsoft Excel. oXL.Quit ' ' Free the object from memory. Set oXL = Nothing End Sub 

在你的方面有一些额外的工作,你也应该能够find命令button。 如果你正在寻找更多的东西,也许更现代的编程语言更适合。