Click here to Skip to main content
15,346,739 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Word
Posted 22 Jul 2011


112 bookmarked

Office Programming Helper Indent VB Code

Rate me:
Please Sign up or sign in to vote.
4.85/5 (35 votes)
1 Mar 2020CPOL5 min read
Indent code, add line numbers, err handling, Add number to words and more, Expose a .NET library to Microsoft Word, Excel, Power Point macros writers and Access VBA code
Indent code, add line numbers, error handling, expose a .NET library and number to words to Microsoft Word, Excel, Power Point macros writers and Access VBA code This application is not just an add-in. It offers many other helpful resources. It provides to you the simplest way to build your add-on

ScreenshotImage 1

System Requirements

  • Windows XP, Vista, 7, 8, 8.1, 10, 11 (32Bit or 64Bit)
  • .NET Framework version 4 or later
  • Any version of Microsoft Office 2007, 2010, 2013, 2016, 2019, 2021 365 (32Bit Or 64Bit)or any single office application such as Word or Excel.

What's New

  • Minimize some system requirement

For Whom This Application is Written

If you are one or more of the following, then I hope that you will find this application helpful:

  • Microsoft Word, Excel and Power Point macros writers
  • MS Access forms designer
  • VBA (Visual Basic for application) Developers
  • Add in builder

How This Application Differs

  • This application is not just an add-in. It offers many other helpful resources.
  • It provides to you the simplest way to build your add-on


Although VB6 went out, VBA is still needed to develop Office macros or Microsoft Access modules so VBA add-in is important.

Using this application to indent your VBA Code and add error handler:

  • Install the app
  • Open an Office document that contains macro and edit the macro in VBA Window it or open Microsoft Access Module.
  • Write your VBA code.
  • From Add-Ins menu, choose Add error handler to this file
  • This will change the format of your code as follows...

Sample of code before applying this add-in:

Public Sub Macro1()
    MsgBox("This is Macro1")
End Sub

Sample of code after applying this add in:

Public Sub Macro1()
    On Error GoTo EorrorHandler
1:  MsgBox("This is Macro1")
    Exit Sub
    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 

Using this application to expose Some .NET to your VBA code

Declaring NP and Number to words Objects

  • Open an Office document that contains macro and edit the macro in VBA Window it or open Microsoft Access Module
  • In VBE Window Select Tools, References, Browse, then select the file
    C:\Program Files\OfficeProgrammingHelper\Bin\OfficeProgrammingHelper.tlb
  • Repeat this for the file
    C:\Program Files\OfficeProgrammingHelper\Bin\NumberToText.tlb
  • Insert the following code in a any VBA Module:
    Public NP As New OfficeProgrammingHelper.NP
  • Now you could use the NP object in any code in your project.

Using NP Object

You could call the NP object in your code to use the functions

  • NP.Clipboard It is used to access computer clipboard. Example: put or get text from the clipboard with specific format, clear the clipboard or converting the text in it form or to Unicode.

  • You could put or get text with the following formats: Text, Unicode text, RTF, HTML.
    Sub Test()
        NP.Clipboard.SetText "Some Text"
        Debug.Print NP.Clipboard.GetText
    End Sub
  • NP.Directory: Create, Delete, Exists, Move
    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: Copy, Create, Delete, Exists, Move, 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


This library will convert numbers to words in English or Arabic and could be used in your Office applications:

Dim c As New NumberToArabicWords

Switch Off the Debug Mode When Executing Office Macro

  • Open an Office document that contains macro and edit the macro in VBA Window it or open Microsoft Access Module
  • From Tools menu, choose your project properties, then choose Protection
  • Check the Lock project for viewing
  • Type a password and click OK
  • Close your document and reopen it

Useful Ready to Import VBA Code

You will find in the bin directory of the application some useful VBA code ready to use:

  • Molecular Weight Calculation code that enables calculating Molecular Weight from the line formula
  • Useful MS Access database function to help in refresh linked tables and to get the next empty integer value for the field in a query and to get LastValue in a table

Expose .NET Controls as ActiveX

This library also provides exposing some .NET controls to be used in Office VBA forms and in ActiveX containers.

Other Useful VBA Add-ins

Here are some useful add-ins you may interested in:

Points of Interest

How to Write a VBA Addin

  • Create a new class library project that uses v2 of .NET Framework and ComVisible
  • The add-in needs either PIA or Visual Studio Tools for Office
  • Add the following references:
    • System
    • System.Windows.Forms
    • Extensibility = Microsoft Add-In Designer
    • Microsoft.Vbe.Interop
    • Microsoft.Office.Core
  • Add a class that Implements IDTExtensibility2
  • Write your code in C# or 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)
    		//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)
    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 
    	//.....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)
    			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
            'save the vb instance
            VBInstance = CType(Application, VBE)
            AddErrorHandlerMenuItem = AddToAddInCommandBar(AddErrorHandlerCaption)
            AddErrorHandlerToProjectMenuItem = _
            'sink the event
            With VBInstance.Events
                Me.AddErrorHandlerEvent = .CommandBarEvents(AddErrorHandlerMenuItem)
                '.....More Code
            End With
        Catch ex As Exception
        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 
        '.....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
  • Generate reg file to register your add-in for VBA like this:
    "FontFace"="Courier New"

    {ClassProgId}, {ClassDescription} and {ClassDisplayName} will be replaced with their values for this addin. {ClassProgId} is the full class name example: VBAErrorHandler.Connect

    Both Addins and Addins64 keys are required for 64 bit addin.

  • Create a setup project that installs and registers the class for com and adds the above reg file to the registry.

How to Analyze the VBA Module

Although we could move from one procedure to another using CodeModule object, the application will be more quick execution than if we get all Module code, then analyze it using Regular Expressions and we use it to analyze each procedure.

Regular Expressions Learning

Many applications may help you learn Regular Expressions:

  1. Expresso form
  2. csharpregexdemo

About Office Addins

Microsoft Office supports add ins written in VBA itself as word add in (*.dotm) and access addin (*.mda) files. Here are some useful application titles for devolving addins:

Related Article


  • 20th July, 2011: Initial version
  • 4th February, 2011: Adding expose .NET library to VBA
  • 21st September, 2012: Adding expose .NET controls and adding installation
  • 7th August, 2013: Some useful additions
  • July, 2015: Color class added
  • January, 2018: Fit with Windows 10
  • 2nd January, 2019
  • 2020-03-01
This article was originally posted at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lebanon Lebanon
ASP.Net Hosting on Linux server
There is a developer behind every piece of code!
DNA is too complex what about it!
No junk DNA; There is a functional role for noncoding DNA

Comments and Discussions

QuestionSource Code Availability Pin
commsjockey2-Mar-20 15:52
Membercommsjockey2-Mar-20 15:52 
AnswerRe: Source Code Availability Pin
NewPast8-Mar-20 6:16
MemberNewPast8-Mar-20 6:16 
Generalms access Pin
Member 145739992-Sep-19 6:31
MemberMember 145739992-Sep-19 6:31 
QuestionWindows 10 Pin
morse10027-Mar-17 9:50
Membermorse10027-Mar-17 9:50 
AnswerRe: Windows 10 Pin
NewPast28-Mar-17 21:49
MemberNewPast28-Mar-17 21:49 
AnswerRe: Windows 10 Pin
NewPast24-Jan-18 0:25
MemberNewPast24-Jan-18 0:25 
GeneralMy vote of 5 Pin
MathildePichard26-Mar-17 1:39
MemberMathildePichard26-Mar-17 1:39 
QuestionMissing Office Applications Pin
cmarcotte1-Mar-15 8:32
Membercmarcotte1-Mar-15 8:32 
AnswerRe: Missing Office Applications Pin
NewPast2-Mar-15 5:56
MemberNewPast2-Mar-15 5:56 
AnswerRe: Missing Office Applications Pin
NewPast4-Mar-15 7:26
MemberNewPast4-Mar-15 7:26 
SuggestionMy vote of 5e6 Pin
VogelH27-Feb-15 1:19
professionalVogelH27-Feb-15 1:19 
AnswerRe: My vote of 5e6 Pin
NewPast1-Mar-15 0:46
MemberNewPast1-Mar-15 0:46 
GeneralMy vote of 5 Pin
JMK8929-Nov-13 5:20
professionalJMK8929-Nov-13 5:20 
GeneralRe: My vote of 5 Pin
NewPast29-Nov-13 5:32
MemberNewPast29-Nov-13 5:32 
QuestionRaise Exception in VBA from Dotnet Pin
Mark Regal17-Jun-13 12:09
MemberMark Regal17-Jun-13 12:09 
AnswerRe: Raise Exception in VBA from Dotnet Pin
NewPast18-Jun-13 4:40
MemberNewPast18-Jun-13 4:40 
GeneralVery good Pin
Slacker00721-Sep-12 1:12
professionalSlacker00721-Sep-12 1:12 
AnswerRe: Very good Pin
NewPast21-Sep-12 8:15
MemberNewPast21-Sep-12 8:15 
QuestionGood work there Pin
Sacha Barber16-Feb-12 0:43
MemberSacha Barber16-Feb-12 0:43 
Questionwork along this line Pin
uildriks19-Oct-11 2:27
professionaluildriks19-Oct-11 2:27 
AnswerThanks for your kind Pin
NewPast19-Oct-11 2:37
MemberNewPast19-Oct-11 2:37 
SuggestionConsider mztools Pin
Tieske819-Aug-11 12:59
professionalTieske819-Aug-11 12:59 
GeneralRe: Consider mztools Pin
NewPast20-Aug-11 19:25
MemberNewPast20-Aug-11 19:25 
GeneralRe: Consider mztools Pin
.:floyd:.22-Jul-15 11:52
Member.:floyd:.22-Jul-15 11:52 
GeneralRe: Consider mztools Pin
Member 123643903-Mar-20 1:07
MemberMember 123643903-Mar-20 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.