如何漂亮地打印 VBA 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2578057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:21:58  来源:igfitidea点击:

How to Pretty print VBA code?

vbams-wordpretty-print

提问by mraviator

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

如何将 VBA 代码复制到 Word 文档中并保留 VBA 编辑器配色方案?

采纳答案by Breakthrough

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!

您可以使用Notepad++以三种方式完成此操作。正如您所知,Notepad++ 是记事本的更高级版本,它支持“开箱即用”的不同代码文件的语法突出显示 - 包括 Visual Basic!

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).

下载并安装它,启动它,然后加载您的 VBA 代码。您应该会自动看到它的漂亮颜色(如果没有,因为文件扩展名不是.vb,请转到 Language -> VB 或 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:

然后,转到插件 - > NppExport。从那里,您可以考虑三个选项:

  • Directly print from Notepad++
  • Copy all formats to clipboard
  • Export to RTF
  • Export to HTML
  • 直接从记事本++打印
  • 将所有格式复制到剪贴板
  • 导出到 RTF
  • 导出为 HTML

The first is self explanatory. The second one - "Copy all formats to clipboard" - will copy the entire file with the highlighted syntaxto 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.

第一个是不言自明的。第二个 - “将所有格式复制到剪贴板” - 将使用突出显示的语法将整个文件复制到剪贴板。单击它后,打开 Microsoft Word 或您喜欢的其他文档编辑器,然后点击粘贴!您应该会看到精美的语法高亮代码。如果出现问题,那么您可以尝试其他选项之一(导出到 RTF/HTML),尽管我从来没有遇到过剪贴板方法的问题。

回答by Henrik Erlandsson

Smart Indentercould 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).

智能压头可能是您正在寻找的东西?您可以将结果加载到 Notepad++ 中,将语言设置为 VB 并另存为 .rtf/.doc(或打印到文件,我一时想不起来了)。

回答by John Wynne

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

对于更现代的方法,Sublime Text 用户可以从 Package Control安装ExportHTML。这具有能够包含行号和更改代码颜色的额外好处。

HTML files can be opened directly within Word.

HTML 文件可以直接在 Word 中打开。

回答by Mehper C. Palavuzlar

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

cnet下载上有两个程序,可以免费试用。如果你没有尝试过,这里是链接:

回答by richj

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

Highlight支持广泛的操作系统和 150 多种语言,包括 Visual Basic,尽管我不确定 VBA。

回答by Wheeliam

I would recommend this one: planetB

我会推荐这个:planetB

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

它适用于 Internet Explorer(对我来说不适用于 Firefox,不确定是不是 Chrome)。

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

另外它是在线的,所以不需要安装任何东西(公司/大学 PC 的情况)

Regards
Wheeliam

回答by Heinzi

The following works with Visual Studio Code:

以下适用于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.
  1. 启动 VS 代码。
  2. 新文件。
  3. 将 VBA 代码复制并粘贴到 VS Code 窗口中。
  4. 在右下角,单击“纯文本”并选择“Visual Basic”。请注意,代码现在是语法突出显示的。
  5. 将格式化的代码复制并粘贴到 Word 中。