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

Controlling the Printing of Highlighting

Using Word's built-in highlighter tool can be a great way to add markup to a document and attract a reader's eyes to ...

Discover More

Inserting the Current Month

Need to add the name of the current month to your document? Word includes a field that can make the addition easy, and it ...

Discover More

Nifty Zooming with the Mouse

Want to use the mouse to control the zoom level for your document? You can do it by combining your mouse use with the ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Counting Open Document Windows

When creating macros, it is sometimes necessary to know how many documents are open in Word. This is relatively easy to ...

Discover More

Determining the Week of the Year

If you are working with dates in a macro, you may need to determine which week of the year a date falls within. This can ...

Discover More

Temporarily Changing the Printer in a Macro

You can use a macro to print to any printer you have defined in Windows. It is good practice, if you are changing which ...

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 two more than 7?

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.