Microsoft KB Archive/315731

= How to retrieve the names of macros from an Excel workbook by using Visual Basic 6.0 =

Article ID: 315731

Article Last Modified on 3/9/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q315731





SUMMARY
This step-by-step article describes how to use Visual Basic 6.0 to retrieve the names of macros from an Excel workbook.

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
 * Excel 2000, Excel 2002, Office Excel 2003, or Office Excel 2007
 * Excel macros
 * Visual Basic for Applications

Create an Excel workbook with two macros
 Start Excel. A new blank workbook is created. Press ALT+F11 to start the Visual Basic Editor. In Project Explorer, double-click ThisWorkbook to start the code editor.  Paste the following code for two simple macros in the code editor: Option Explicit

Sub Macro_A MsgBox &quot;This is Macro A&quot; End Sub

Sub Macro_B MsgBox &quot;This is Macro B&quot; End Sub  Close the Visual Basic Editor, and return to the spreadsheet view. In Excel 2003 and in earlier versions of Excel, save the workbook as C:\Abc.xls.

In Excel 2007, save the workbook as a macro-enabled workbook that is named C:\Abc.xlsm. Close the workbook and quit Excel.</li></ol>

Create a Visual Basic application to list the macros in the workbook
<ol> In Visual Basic 6.0, create a new Standard EXE project.</li> On the Project menu, click References. In the References dialog box, select the following references: <ul> Microsoft Visual Basic for Applications Extensibility 5.3</li> For Microsoft Excel 2000:

Microsoft Excel 9.0 Object Library</li> For Microsoft Excel 2002:

Microsoft Excel 10.0 Object Library</li> For Microsoft Office Excel 2003:

Microsoft Excel 11.0 Object Library</li> For Microsoft Office Excel 2007:

Microsoft Excel 12.0 Object Library</li></ul> </li> Click OK.</li> Add a button to the form. The button has the default name Command1.</li> Add a list box to the form. The list box has the default name List1.</li>  Define a click event handler procedure for the button. Use the following code for this procedure, to display information about the macros that are defined in C:\Abc.xls: Private Sub Command1_Click ' 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(&quot;C:\ABC.XLS&quot;) ' 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 <> &quot;&quot; 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 </li></ol>

Test the sample
<ol> Build and run the application.</li>  Click the command button. The list box displays the names of all of the macros and the workbook that contains them, as follows: <pre class="fixed_text">ThisWorkbook   Macro_A ThisWorkbook   Macro_B </li></ol>

Troubleshooting
Because of enhanced security provisions in Excel 2002, Excel 2003, and Excel 2007, you may receive the following error message from the Visual Basic program when you use Excel 2002, Excel 2003, or Excel 2007:

Programmatic access to Visual Basic Project is not trusted.

For more information about this problem and how to resolve it, click the following article number to view the article in the Microsoft Knowledge Base:

282830 PRB: Programmatic access to Office XP VBA project is denied

<div class="references_section">