Skip to content

VBA API Cookbook & Recipes for XLS Padlock

This topic provides a comprehensive collection of VBA code snippets (“recipes”) for advanced control over your protected application at runtime. Using the XLS Padlock VBA API, you can retrieve system information, manage saving and loading, customize the user interface, and interact with online features.

The two main API objects are:, Application.COMAddIns("GXLSForm.GXLSFormula").Object for most runtime information., Application.COMAddIns("GXLS.GXLSPLock").Object for saving-related operations.

Each recipe below provides a ready-to-use VBA function or subroutine.


These recipes help you retrieve information about the application’s environment and status.

Use this to confirm that your code is running inside a protected application created by XLS Padlock.

Public Function XLSPadlockAvailable() As Boolean
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
If Not XLSPadlock Is Nothing Then
XLSPadlockAvailable = XLSPadlock.IsProtected()
Else
XLSPadlockAvailable = False
End If
End Function

Retrieves the filename of the running .exe file.

Public Function GetEXEFilename() As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
GetEXEFilename = XLSPadlock.PLEvalVar("EXEFilename")
Exit Function
Err:
GetEXEFilename = ""
End Function

Retrieves the version strings you defined in the “EXE Version Info” section of XLS Padlock.

Public Function ReadVersionInfo(infoType As String) As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
ReadVersionInfo = XLSPadlock.PLEvalVar(infoType)
Exit Function
Err:
ReadVersionInfo = ""
End Function

Usage:, ReadVersionInfo("ProductVersion") returns the Product Version., ReadVersionInfo("FileVersion") returns the File Version.

Retrieves the unique System ID required for hardware-locked activation keys. This is the ID your customers will send you.

Public Function ReadSystemID() As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
ReadSystemID = XLSPadlock.PLEvalVar("SystemID")
Exit Function
Err:
ReadSystemID = ""
End Function

Retrieves command-line parameters passed to your application’s .exe file.

Public Function ReadParamStr(index As Integer) As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
ReadParamStr = XLSPadlock.PLEvalVar("ParamStr" & index)
Exit Function
Err:
ReadParamStr = ""
End Function

Usage: ReadParamStr(1) returns the first parameter, ReadParamStr(2) the second, and so on.


Manage secure save files (.xlsc or .xlsce) programmatically.

Retrieves the full path to the secure save file that is currently loaded.

Public Function GetSecureWorkbookFilename() As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
GetSecureWorkbookFilename = XLSPadlock.GetSaveFilename()
Exit Function
Err:
GetSecureWorkbookFilename = ""
End Function

Retrieves the path to the local folder where XLS Padlock stores your application’s settings and secure save files.

Public Function GetStoragePath() As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
GetStoragePath = XLSPadlock.PLEvalVar("SPath")
Exit Function
Err:
GetStoragePath = ""
End Function

XLS Padlock can automatically call the XLSPadlock_OnAfterSave subroutine after a user saves their work. This is useful for logging, showing a confirmation message, or other post-save actions.

Sub XLSPadlock_OnAfterSave(SaveFilename As String)
MsgBox "The workbook has been successfully saved as: " & SaveFilename
End Sub

Programmatically open a secure save file.

Public Sub LoadXLSPadlockSaveFile(FilePath As String)
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
If Not XLSPadlock Is Nothing Then
XLSPadlock.PLOpenSaveFile FilePath
End If
End Sub
' Example of a caller function that shows a file browser:
Sub Load_Old_Save()
Dim strFileToOpen As String
strFileToOpen = Application.GetOpenFilename( _
Title:="Please choose a save file to open", _
FileFilter:="Save Files (*.xlsc),*.xlsc")
If strFileToOpen <> "False" Then
LoadXLSPadlockSaveFile strFileToOpen
End If
End Sub

Set the default filename that appears in the “Save As” dialog box.

Public Sub SetSecureWorkbookFilename(Filename As String)
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
If Not XLSPadlock Is Nothing Then
XLSPadlock.SetDefaultSaveFilename Filename
End If
End Sub
' Example: Set the default name to "my save.xlsc"
' Call SetSecureWorkbookFilename("D:\My Documents\my save.xlsc")

Save a secure copy of the workbook directly to a specified file path without showing the “Save As” dialog.

Public Function SaveSecureWorkbookToFile(Filename As String) As Boolean
Dim XLSPadlock As Object
Dim HadPrevious As Boolean
Dim PreviousTime As Date
On Error GoTo Err
If Len(Filename) = 0 Then Exit Function
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
' Remember the state of the target file before saving.
HadPrevious = (Len(Dir(Filename)) > 0)
If HadPrevious Then PreviousTime = FileDateTime(Filename)
' SaveWorkbook is a Sub: call it as a statement.
XLSPadlock.SaveWorkbook Filename
' Success: the file exists and, if it already existed, it was rewritten.
If Len(Dir(Filename)) > 0 Then
If Not HadPrevious Then
SaveSecureWorkbookToFile = True
ElseIf FileDateTime(Filename) <> PreviousTime Then
SaveSecureWorkbookToFile = True
End If
End If
Exit Function
Err:
SaveSecureWorkbookToFile = False
End Function
' Example: Save the workbook directly
' If SaveSecureWorkbookToFile("D:\My Documents\my save.xlsc") Then
' MsgBox "Workbook saved successfully."
' End If

Save and Load Cell Values Programmatically

Section titled “Save and Load Cell Values Programmatically”

In Save defined cell values only mode (.xlsce), two methods let you save the marked cell values to a file and load them back. LoadCellValuesFromFile restores the values into the Excel session that is already running, without opening a new instance: it is the programmatic counterpart of the full-save behavior described in Open an Existing Save File with VBA. Both methods are Subs: call them as statements.

Public Sub SaveCellValuesTo(Filename As String)
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
If Not XLSPadlock Is Nothing Then
XLSPadlock.SaveCellValuesToFile Filename
End If
End Sub
Public Sub LoadCellValuesFrom(Filename As String)
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
If Not XLSPadlock Is Nothing Then
XLSPadlock.LoadCellValuesFromFile Filename
End If
End Sub

If you release a new version of your workbook, you might need to import data from a save file created with a previous version.

Learn how to migrate user data with VBA


Customize UI elements like dialog boxes.

Programmatically Hide Wait/Loading Dialogs

Section titled “Programmatically Hide Wait/Loading Dialogs”

You can hide the “Loading workbook, please wait…” dialog box with a VBA call instead of waiting for it to auto-close.

Public Sub HideLoadingDialog()
Dim XLSPadlock As Object
On Error Resume Next
Set XLSPadlock = Application.COMAddIns("GXLS.GXLSPLock").Object
If Not XLSPadlock Is Nothing Then
' A call with Option "3" and Value "0" hides the dialog.
XLSPadlock.SetOption Option:="3", Value:="0"
End If
End Sub

Interact with XLS Padlock’s online features.

Tests whether an active internet connection is available on the user’s computer.

Public Function IsInternetAvailable() As Boolean
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
IsInternetAvailable = (XLSPadlock.PLEvalVar("InternetConnected") = "1")
Exit Function
Err:
IsInternetAvailable = False
End Function

Retrieves a hash of the activation token returned by the web server after a successful online activation.

Public Function GetValidationToken() As String
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
GetValidationToken = XLSPadlock.PLEvalVar("ValidationToken")
Exit Function
Err:
GetValidationToken = ""
End Function

Gets the result of the last online validation attempt.

Public Function IsValidationOK() As Boolean
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
IsValidationOK = (XLSPadlock.PLEvalVar("ValidationSuccess") = "1")
Exit Function
Err:
IsValidationOK = False
End Function