A VBA Procedure To Open A File In Windows Notepad

As part of my recent work for a client request, refer to my previous post:

I tried to explore Notepad’s command-line switches.

Information on this is relatively hard to find, typical poor support documentation from Microsoft!, but this is what I could find from various sources:

  • /A <filename> open file as ansi
  • /W <filename> open file as unicode
  • /P <filename> print filename
  • /PT <filename> <printername> <driverdll> <port> print filename to designated printer

So as you can see, it is possible for Notepad to launch in either ANSI or Unicode modes!

Launching Notepad Using VBA

Armed with this information, it was simply a question of building a simple wrapper procedure and below is what I came up with:

Enum NotepadEncoding
    ANSIEncoding = 1
    UNICODEEncoding = 2
End Enum


'---------------------------------------------------------------------------------------
' Procedure : Notepad_OpenTextFile
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open notepad, and optional load the specified file
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
' Dependencies: NotepadEncoding Enum
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile     : (Optional) File to load when Notepad is launched
' lEncoding : (Optional) Encoding to use, default is ANSI
'
' Usage Examples:
' ~~~~~~~~~~~~~~~
' Launch Notepad
' Call Notepad_OpenTextFile
'
' Launch Notepad and open a file
' Call Notepad_OpenTextFile("C:\..\..\YourFilename.log")
'
' Launch Notepad open a file in Unicode mode
' Call Notepad_OpenTextFile("C:\..\..\YourFilename.log", UNICODEEncoding)
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2024-05-11              Initial Release
'---------------------------------------------------------------------------------------
Sub Notepad_OpenTextFile(Optional sFile As String, _
                         Optional lEncoding As NotepadEncoding = ANSIEncoding)
    On Error GoTo Error_Handler

    If sFile = "" Then
        Shell "notepad.exe", vbNormalFocus
    Else
        Shell "notepad.exe /" & IIf(lEncoding = ANSIEncoding, "A", "W") & _
              " """ & sFile & """", vbNormalFocus
    End If

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: Notepad_OpenTextFile" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

I’ve defaulted lEncoding to ANSI because that is Notepad’s default encoding on Windows, so it will be what most people are familiar with. That’s said, you can simply switch it to Unicode should you need such encoding for your project.

Usage Examples

Then to use the procedure you could do something like:

Launch Notepad

Call Notepad_OpenTextFile

Launch Notepad and open a file

Call Notepad_OpenTextFile("C:\..\..\YourFilename.log")

Launch Notepad open a file in Unicode mode

Call Notepad_OpenTextFile("C:\..\..\YourFilename.txt", UNICODEEncoding)

The beauty here, once again, is this should work in any VBA program (Access, Excel, Word, …).