How to Pretty print VBA code?
Asked Answered
C

7

44

How do you copy VBA code into a Word document and retain the VBA editor color scheme?

Centistere answered 5/4, 2010 at 11:20 Comment(4)
Does it need to be in Word? Do you just need it to be printed out with color syntax highlighting?Unyielding
That would be "pretty paste"... or "pretty load"? Notice how "pretty print" confused some into telling you how to send your colored code to the printer!Quarterstaff
If one of the answers helped you, would you consider choosing it as the answer?Malonis
@Jean-FrançoisCorbett "prettyprint" is a pretty established synonym for "code beautifier". I wasn't confused at all.Obliquity
S
44

You can use Notepad++ to accomplish this in three ways. Just so you know, Notepad++ is a more advanced version of Notepad, which supports syntax highlighting of different code files "out of the box" - Visual Basic included!

Download & install it, fire it up, and load up your VBA code. You should automatically see it beautifully coloured (if not, because the file extension is something other than .vb, go to Language -> VB or Language -> V -> VB).

If you need to change any of the colours, you can easily do so - just go to Settings -> Styler Configurator. From that menu, you can change the various highlighting and font options, to suit your needs - although the default usually suffices for most.

Then, go to Plugins -> NppExport. From there, you have three options you can consider:

  • Directly print from Notepad++
  • Copy all formats to clipboard
  • Export to RTF
  • Export to HTML

The first is self explanatory. The second one - "Copy all formats to clipboard" - will copy the entire file with the highlighted syntax to the clipboard. Once you click it, then open Microsoft Word or your other favourite document editor, and just hit paste! You should see the beautifully syntax-highlighted code. If something goes wrong, then you can try one of the other options (export to RTF/HTML), although I've never had a problem with the clipboard method.

Sharitasharity answered 30/6, 2011 at 15:4 Comment(3)
Only valid answer so far! +1 Everyone else: OP is asking how to put the code in [Microsoft] Word.Quarterstaff
This does not work in October 2019, it exports just black text now.Abroms
It works in 2024 as outlined by Breakthrough.Strain
T
2

There are two programs on cnet downloads, which are free to try. In case you did not try them, here are the links:

Teodoor answered 5/4, 2010 at 11:39 Comment(1)
I use O365. do you have experience with one of those VBA formatters?Germayne
O
2

Smart Indenter could be what you're looking for? You'd load the result into Notepad++, set language to VB and save as .rtf/.doc (or print to file, can't remember off the top of my head).

Obliquity answered 13/6, 2012 at 9:45 Comment(0)
V
2

For a more modern approach, Sublime Text users can install ExportHTML from Package Control. This has the added benefit of being able to include the line numbers, and changing the code coloration.

HTML files can be opened directly within Word.

Valley answered 28/2, 2018 at 3:29 Comment(0)
G
1

Highlight supports a wide range of Operating Systems and 150+ languages including Visual Basic, although I'm not sure about VBA.

Goode answered 5/4, 2010 at 11:40 Comment(0)
B
1

I would recommend this one: planetB

It works well with Internet Explorer (didn't work wit Firefox for me, not sure abt Chrome).

Plus it's online, so no need to install anything (the case of Company/University PC's)

Regards
Wheeliam
Burgh answered 3/9, 2018 at 15:25 Comment(0)
S
1

The following works with Visual Studio Code:

  1. Start VS Code.
  2. New file.
  3. Copy & Paste the VBA code into the VS Code window.
  4. In the lower right-hand corner, click on "Plain Text" and select "Visual Basic" instead. Note that the code is now syntax-highlighted.
  5. Copy & Paste the formatted code into Word.
Swiftlet answered 28/8, 2019 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.