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

Inconsistent Adding of Words to a Custom Dictionary

Custom dictionaries are a great way to adapt the spelling and grammar checkers to your needs. If you find that Word isn't ...

Discover More

Finding Wayward Links

Combining workbooks that have cross-links to each other can offer some special challenges. This tip examines how you can ...

Discover More

Changing References in a Lot of Defined Names

Need to change some cell references in your defined names? Changing one or two is easy; changing dozens is a good deal ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Clearing the Undo Stack in a Macro

When writing a macro, you may need a way to clear the undo stack. This can be done with a single command, as described in ...

Discover More

Moving the Insertion Point to the End of a Line

When writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. ...

Discover More

Changing What Is Pasted in a Dialog Box

When you record a macro, Word very literally records what you do. This includes filling in various settings in dialog ...

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 4 + 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.