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

Determining the Length of a Text File

When processing plain text files in a macro, it is often helpful to know how much data the file contains. The normal way ...

Discover More

Saving All Open Documents

Got a lot of open documents you are working with? You can save them all at one time by adding a handy tool to your Quick ...

Discover More

Limiting Directories in the FILENAME Field

When you use the FILENAME field in a document, it can include the full path name that leads to your file. This might be ...

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)

Occurrences of a Text String within a Document

You may have a need to find out how many times a certain text string occurs within a document. You can find out manually ...

Discover More

Determining the Size of a File

When processing a document using a macro, you may need to know the precise size of a particular file. The way you figure ...

Discover More

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
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 2 + 2?

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.