Office 编程助手缩进 VB 代码
缩进代码、添加行号、错误处理、将数字转换为文本等,为 Microsoft Word、Excel、Power Point 宏编写者和 Access VBA 代码公开 .NET 库
系统要求
- Windows XP, Vista, 7, 8, 8.1, 10 (32 位或 64 位), 11
- .NET Framework 版本 4.8
https://dotnet.microsoft.com/en-us/download/dotnet-framework/net48 - Microsoft 365 或 Office 2007、2010、2013、2016、2019、2021 (32 位或 64 位) 的任何版本,或任何单个 Office 应用程序,如 Word 或 Excel
有什么新内容
- 现已支持 Windows 11
此应用程序是为谁编写的
如果您符合以下一个或多个条件,我希望您会发现此应用程序很有帮助
- Microsoft Word、Excel 和 PowerPoint 宏编写者
- MS Access 表单设计器
- VBA (Visual Basic for Application) 开发人员
- 插件生成器
此应用程序有何不同
- 此应用程序不仅仅是一个插件。它提供了许多其他有用的资源。
- 它为您提供了构建附加组件的最简单方法
背景
尽管 VB6 已淘汰,但 VBA 仍然是开发 Office 宏或 Microsoft Access 模块所必需的,因此 VBA 插件很重要。
使用此应用程序缩进 VBA 代码并添加错误处理程序
- 安装应用程序。
- 打开包含宏的 Office 文档,在 VBA 窗口中编辑宏,或打开 Microsoft Access 模块。
- 编写您的 VBA 代码。
- 从“加载项”菜单中,选择“为此文件添加错误处理程序”。
- 这将按如下方式更改您的代码格式……
应用此插件前代码示例
Public Sub Macro1()
MsgBox("This is Macro1")
End Sub
应用此插件后代码示例
Public Sub Macro1()
On Error GoTo EorrorHandler
1: MsgBox("This is Macro1")
Exit Sub
EorrorHandler:
Debug.Print "Error in: Module1.Macro1." & Erl & vbNewLine & Err.Description
'This will print the error Module name and the Error Sub Name and
'line number in the immediate window and this is useful in debugging
Debug.Assert False
'This will stop the execution of the code if you are in debug mode and
'has no effect in run mode
MsgBox "Error in: Module1.Macro1." & Erl & vbNewLine & Err.Description
'This will show a message box about the error in run time
End Sub
使用此应用程序将某些 .NET 公开到您的 VBA 代码
声明 NP 和 Number to Words 对象
- 打开包含宏的 Office 文档,在 VBA 窗口中编辑宏,或打开 Microsoft Access 模块。
- 在 VBE 窗口中,选择 **工具**、**引用**、**浏览**,然后选择文件
C:\Program Files\OfficeProgrammingHelper\Bin\OfficeProgrammingHelper.tlb - 对文件重复此操作
C:\Program Files\OfficeProgrammingHelper\Bin\NumberToText.tlb - 将以下代码插入任何 VBA 模块
Public NP As New OfficeProgrammingHelper.NP
- 现在您可以在项目中的任何代码中使用 NP 对象。
使用 NP 对象
您可以在代码中调用 NP 对象来使用函数
-
NP.Clipboard
:用于访问计算机剪贴板。示例:以特定格式从剪贴板获取或放入文本、清空剪贴板或将其文本转换为其格式或 Unicode。
- 您可以使用以下格式放置或获取文本:文本、Unicode 文本、RTF 和 HTML。
Sub Test() NP.Clipboard.Clear NP.Clipboard.SetText "Some Text" NP.Clipboard.Ascii2Unicode NP.Clipboard.Unicode2Ascii Debug.Print NP.Clipboard.GetText End Sub
NP.Directory
:创建
、删除
、存在
、移动
Sub Test() If Not NP.Directory.Exists("C:\Temp") Then NP.Directory.CreateDirectory "C:\Temp" NP.Directory.Move "C:\Temp", "C:\Temp2" NP.Directory.Delete "C:\Temp2" End Sub
NP.File
:复制
、创建
、删除
、存在
、移动
、Shell
Sub Test() If Not NP.File.Exists("C:\Temp.txt") Then NP.File.Create "C:\Temp.txt" NP.File.Move "C:\Temp.txt", "C:\Temp2.txt" NP.File.Copy "C:\Temp2.txt", "C:\Temp.txt" NP.File.Delete "C:\Temp2.txt" NP.Shell "C:\Temp.txt" End Sub
NP.Screen
:CM
、Height
、Width
、TwipsPerPixelX
、TwipsPerPixelY
NP.Text
:EncodingConvert
、EncodingConvertByCodePage
、GetTextHeight
、Md5Hash
、TrimAny
NP.SQL
NumbersToText
此库会将数字转换为英文或阿拉伯文单词,并可在您的 Office 应用程序中使用
Dim c As New NumberToArabicWords
Debug.Print(c.ConvertToWords(10002015))
执行 Office 宏时关闭调试模式
- 打开包含宏的 Office 文档,在 VBA 窗口中编辑宏,或打开 Microsoft Access 模块。
- 在 **工具** 菜单中,选择 **您的项目属性**,然后选择 **保护**
- 勾选 **锁定项目以供查看**
- 输入密码,然后点击 **确定**
- 关闭您的文档并重新打开它
有用的即插即用 VBA 代码
您将在应用程序的 *bin* 目录中找到一些有用的 VBA 代码,可供使用
- 分子量计算代码,可根据线式方程式计算分子量
- 有用的 MS Access 数据库函数,可帮助刷新链接表、获取查询字段的下一个空整数值以及获取表中的 `LastValue`
将 .NET 控件公开为 ActiveX
此库还公开了一些 .NET 控件,可在 Office VBA 表单和 ActiveX 容器中使用。
其他有用的 VBA 加载项
以下是一些您可能感兴趣的有用加载项
关注点
如何编写 VBA 加载项
- 创建一个使用 V 4.8 .NET Framework 并启用 ComVisible 的新类库项目。
- 添加以下引用
系统
System.Windows.Forms
- Extensibility = Microsoft Add-In Designer
Microsoft.Vbe.Interop
Microsoft.Office.Core
- 添加一个实现 `IDTExtensibility2` 的类。
- 用 C# 或 VB 编写您的代码
private const string AddErrorHandlerCaption = "Add error handler to this file"; private CommandBarControl AddErrorHandlerMenuItem; private CommandBarEvents AddErrorHandlerEvent; void IDTExtensibility2.OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref System.Array custom) { this.OnConnection(Application, ConnectMode, AddInInst, ref custom); } private void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref System.Array custom) { try { //save the VB instance VBInstance = (VBE)Application; AddErrorHandlerMenuItem = AddToAddInCommandBar(AddErrorHandlerCaption); AddErrorHandlerToProjectMenuItem = AddToAddInCommandBar(AddErrorHandlerToProjectCaption); //sink the event this.AddErrorHandlerEvent = VBInstance.Events.CommandBarEvents(AddErrorHandlerMenuItem); //.....More Code } catch (Exception ex) { ErrMsg(ex); } } void IDTExtensibility2.OnDisconnection (ext_DisconnectMode RemoveMode, ref System.Array custom) { this.OnDisconnection(RemoveMode, ref custom); } private void OnDisconnection (ext_DisconnectMode RemoveMode, ref System.Array custom) { //INSTANT C# TODO TASK: The 'On Error Resume Next' //statement is not converted by Instant C#: On Error Resume Next //delete the command bar entry AddErrorHandlerMenuItem.Delete(); //.....More Code //shut down the Add-In } private void OnAddErrorClick (object CommandBarControl, ref bool handled, ref bool CancelDefault) { //.....Your click event code here } //INSTANT C# NOTE: Converted event handler wireups: private bool EventsSubscribed = false; private void SubscribeToEvents() { if (EventsSubscribed) return; else EventsSubscribed = true; AddErrorHandlerEvent.Click += OnAddErrorClick; }
Private Const AddErrorHandlerCaption As String = "Add error handler to this file" Private AddErrorHandlerMenuItem As CommandBarControl Private WithEvents AddErrorHandlerEvent As CommandBarEvents Private Sub OnConnection(ByVal Application As Object _ , ByVal ConnectMode As ext_ConnectMode, ByVal AddInInst As Object _ , ByRef custom As System.Array) Implements IDTExtensibility2.OnConnection Try 'save the vb instance VBInstance = CType(Application, VBE) AddErrorHandlerMenuItem = AddToAddInCommandBar(AddErrorHandlerCaption) AddErrorHandlerToProjectMenuItem = _ AddToAddInCommandBar(AddErrorHandlerToProjectCaption) 'sink the event With VBInstance.Events Me.AddErrorHandlerEvent = .CommandBarEvents(AddErrorHandlerMenuItem) '.....More Code End With Catch ex As Exception ErrMsg(ex) End Try End Sub Private Sub OnDisconnection(ByVal RemoveMode As ext_DisconnectMode _ , ByRef custom As System.Array) _ Implements IDTExtensibility2.OnDisconnection On Error Resume Next 'delete the command bar entry AddErrorHandlerMenuItem.Delete() '.....More Code 'shut down the Add-In End Sub Private Sub OnAddErrorClick(ByVal CommandBarControl As Object, _ ByRef handled As Boolean, _ ByRef CancelDefault As Boolean) Handles AddErrorHandlerEvent.Click '.....Your click event code here End Sub
- 生成一个 reg 文件,以像这样注册您的 VBA 加载项
REGEDIT4 [HKEY_CURRENT_USER\Software\Microsoft\VBA\VBE\6.0\Addins\{ClassProgId}] "CommandLineSafe"=dword:00000000 "Description"="{ClassDescription}" "FriendlyName"="{ClassDisplayName}" "LoadBehavior"=dword:00000003 [HKEY_CURRENT_USER\Software\Microsoft\VBA\VBE\6.0\Addins64\{ClassProgId}] "CommandLineSafe"=dword:00000000 "Description"="{ClassDescription}" "FriendlyName"="{ClassDisplayName}" "LoadBehavior"=dword:00000003 [HKEY_CURRENT_USER\Software\Microsoft\VBA\6.0\Common] "FontFace"="Courier New" "FontHeight"="10"
{ClassProgId}
、{ClassDescription}
和{ClassDisplayName}
将被替换为该加载项的值。{ClassProgId}
是完整的类名,例如:VBAErrorHandler.Connect
对于 64 位加载项,需要 `Addins` 和 `Addins64` 注册表项。
- 创建一个设置项目,安装并注册 COM 类,并将上述 reg 文件添加到注册表中。
如何分析 VBA 模块
虽然我们可以使用 `CodeModule` 对象在过程之间移动,但如果获取整个模块代码,然后使用正则表达式进行分析,并用它来分析每个过程,应用程序的执行速度会更快。
正则表达式学习
许多应用程序可以帮助您学习正则表达式
- Expresso 来自 http://www.ultrapico.com
- csharpregexdemo
关于 Office 加载项
Microsoft Office 支持使用 VBA 本身编写的加载项,如 Word 加载项(*.dotm*)和 Access 加载项(*.mda*)文件。以下是一些用于开发加载项的有用应用程序标题
相关文章
历史
- 2011 年 7 月 20 日:初始版本
- 2011 年 2 月 4 日:添加将 .NET 库公开到 VBA
- 2012 年 9 月 21 日:添加公开 .NET 控件并添加安装
- 2013 年 8 月 7 日:一些有用的添加
- 2015 年 7 月:添加了 Color 类
- 2018 年 1 月:兼容 Windows 10
- 2019 年 1 月 2 日
- 2020 年 3 月 1 日
- 2023/08/12 版本 4