Printing the Code for a Macro Procedure

Written by Allen Wyatt (last updated September 11, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

Phil uses macros quite a lot and would like to know if it is possible to read the VBA code in his modules from within a macro. His immediate wish is to create a macro to print out the code for a given Sub or Function. Currently Phil copies and pastes the code to a Word document so he can print, but he finds this tedious and repetitive.

There are a few ways you can print your macro code. The easiest (at least for some versions of Word) is to display, in the Visual Basic Editor, the module containing the macro procedure and then choose Print from the File menu. Word then prints the entire module to your default printer.

Unfortunately, this intuitive approach isn't available in all versions of Word. The next easiest approach is to export the module containing the procedure you want to print. You can do this by right-clicking the name of the module in the Visual Basic Explorer. In the resulting Context menu, choose Export Module. You'll see a standard "Save" dialog box, which you can use to specify where and under what name the module should be exported.

The exported module will have a filename extension of .BAS. Even so, the module is nothing but a text file. You could load it in any text editor desired or even into Word itself. You don't need to rename the file to have a .TXT filename extension, but you could if you wanted to. Once loaded into a text editor (or into Word), you can delete any procedures you don't want to print, set formatting parameters (such as font size), and do your printing.

If you want to automate the procedure even more, you could create a macro that will, programmatically, access your macro code and create a new Word document for each module in the active document, and then place the macro code from that module into the new document.

Sub ModsToDocs()

' Microsoft Visual Basic for Applications Extensibility 5.3
' must be set in Tools > References otherwise an error will occur

    Dim vbComp As VBIDE.VBComponent
    Dim docPath As String

    docPath = ActiveDocument.Path & "\"
    For Each vbComp In ActiveDocument.VBProject.VBComponents
        If vbComp.Type = vbext_ct_StdModule Then
            Documents.Add
            Selection.TypeText vbComp.CodeModule.Lines(1, _
              vbComp.CodeModule.CountOfLines)
            ActiveDocument.SaveAs2 FileName:=docPath & vbComp.Name
            ActiveDocument.Close
        End If
    Next vbComp
End Sub

In order for the macro to work, you'll need to choose References from the Tools menu (in the Visual Basic Editor) and make sure the Microsoft Visual Basic for Applications Extensibility 5.3 library is selected. Once the macro is done running, you'll have a document, saved to disk, for each module of macro code. The documents are saved into the same folder as the original document itself. (This means that you should run the macro only on a document that was previously saved to disk.)

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11298) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving in a Macro Using a Desired File Name

Need to save a new document, from within a macro, to a specific file name? If you use the Record Macro capabilities of ...

Discover More

Multiple Data Points in a Chart Column

Excel provides lots of ways you can create charts. This tip provides some pointers on how you can combine stacked column ...

Discover More

Freezing Cell Size when Inserting Pictures

Insert a picture into a table cell, and you may quickly find that the table is no longer the size you expected. Here's ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Repeating In a Macro

Macros are often used to process information stored in documents. Usually the processing involves some sort of iterative ...

Discover More

Assigning a Macro to a Shortcut Key

Do you have a macro that you use frequently? Using the File menu to access the macro can be time consuming. This tip ...

Discover More

Can't Edit Macros

Load up documents created on older versions of Word, and you may find that you can't edit the macros you are used to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 5?

2021-09-11 22:33:26

Phil Maier

Thanks Alan, Peter Roth, Robert Love, and Ken Endacott for the excellent feedback. The Visual Basic for Applications Extensibility is a new world for me. I now have enough reading and experimenting ahead to keep me occupied for some time. All the best, Phil.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.