Written by Allen Wyatt (last updated September 11, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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.
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!
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 MoreWhen processing a document using a macro, you may need to know the precise size of a particular file. The way you figure ...
Discover MoreWhen creating macros, it is sometimes necessary to know how many documents are open in Word. This is relatively easy to ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments