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.
Application & System Information
Section titled “Application & System Information”These recipes help you retrieve information about the application’s environment and status.
Check if the Workbook is Protected
Section titled “Check if the Workbook is Protected”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 IfEnd FunctionGet the Application’s EXE Filename
Section titled “Get the Application’s EXE Filename”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 FunctionErr: GetEXEFilename = ""End FunctionGet EXE File and Product Version
Section titled “Get EXE File and Product Version”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 FunctionErr: ReadVersionInfo = ""End FunctionUsage:, ReadVersionInfo("ProductVersion") returns the Product Version., ReadVersionInfo("FileVersion") returns the File Version.
Get the User’s System ID
Section titled “Get the User’s System ID”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 FunctionErr: ReadSystemID = ""End FunctionGet Command-Line Parameters
Section titled “Get Command-Line Parameters”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 FunctionErr: ReadParamStr = ""End FunctionUsage: ReadParamStr(1) returns the first parameter, ReadParamStr(2) the second, and so on.
Saving & Loading Workbooks
Section titled “Saving & Loading Workbooks”Manage secure save files (.xlsc or .xlsce) programmatically.
Get Current Save File Path
Section titled “Get Current Save File Path”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 FunctionErr: GetSecureWorkbookFilename = ""End FunctionGet Local Save Folder Path
Section titled “Get Local Save Folder Path”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 FunctionErr: GetStoragePath = ""End FunctionExecute VBA Code After Saving
Section titled “Execute VBA Code After Saving”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: " & SaveFilenameEnd SubOpen an Existing Save File with VBA
Section titled “Open an Existing Save File with VBA”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 IfEnd 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 IfEnd SubSuggest a Filename for the Save Dialog
Section titled “Suggest a Filename for the Save Dialog”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 IfEnd Sub
' Example: Set the default name to "my save.xlsc"' Call SetSecureWorkbookFilename("D:\My Documents\my save.xlsc")Save a Secure Copy without Prompt
Section titled “Save a Secure Copy without Prompt”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 FunctionErr: SaveSecureWorkbookToFile = FalseEnd Function
' Example: Save the workbook directly' If SaveSecureWorkbookToFile("D:\My Documents\my save.xlsc") Then' MsgBox "Workbook saved successfully."' End IfSave 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 IfEnd 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 IfEnd SubMigrate User Data from a Previous Version
Section titled “Migrate User Data from a Previous Version”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
User Interface
Section titled “User Interface”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 IfEnd SubOnline Activation & Validation
Section titled “Online Activation & Validation”Interact with XLS Padlock’s online features.
Check for an Internet Connection
Section titled “Check for an Internet Connection”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 FunctionErr: IsInternetAvailable = FalseEnd FunctionGet the Online Activation Token
Section titled “Get the Online Activation Token”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 FunctionErr: GetValidationToken = ""End FunctionCheck if Online Validation Succeeded
Section titled “Check if Online Validation Succeeded”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 FunctionErr: IsValidationOK = FalseEnd Function