Skip to content

Protect a Word Document

You have built a Word document with VBA macros — maybe it generates contracts from templates, fills in client details automatically, or produces formatted reports — and you need to distribute it without exposing your source code.

In this tutorial, you’ll walk through the complete workflow: compiling your VBA logic into a protected DLL, wiring it up to Word through the auto-generated VBA Bridge, and optionally adding activation keys with hardware locking.

Time needed: 15-20 minutes


Imagine you sell a “Document Builder” tool: users open a Word template containing placeholders like {CUSTOMER_NAME}, click a button, and the macro fills everything in, adds a formatted table, and exports the result to PDF.

VBA Padlock allows you to commercialize this tool while keeping your proprietary logic completely hidden inside a native DLL.


Start by opening your Word file in VBA Padlock to create a project that will hold your compiled code and licensing settings.

  1. Launch VBA Padlock from the Start menu.

  2. Click “Open Office File” in the ribbon and select your .docm or .dotm file (for example, DocumentBuilder.docm).

    VBA Padlock Welcome Screen

  3. Set Project Information

    In the Project Info tab:

    • Application Title: Give your tool a name (e.g., Document Builder).
    • Security Code: Click Generate to create the unique cryptographic link between your document and the DLL.

    Project Information Settings


Switch to the Code Editor. This is where you write the VBA logic you want to hide. Below is an excerpt based on the real DocumentBuilder example included with VBA Padlock.

VBA Padlock Code Editor

' Main.bas -- Compiled into the DLL
' These functions access Word's object model via the "Application" object.
Sub HelloWorld()
MsgBox "Hello from VBA Padlock!" & Chr(13) & Chr(13) & _
"Document Builder is ready.", _
vbInformation, "VBA Padlock - Word"
End Sub
'-------------------------------------------------------------------------------
' Get current document information
' Returns: Formatted string with document stats
'-------------------------------------------------------------------------------
Function GetDocumentInfo()
Dim Doc
Dim Info
Set Doc = Application.ActiveDocument
Info = "Document: " & Doc.Name & Chr(13)
Info = Info & "Pages: " & Doc.ComputeStatistics(wdStatisticPages) & Chr(13)
Info = Info & "Words: " & Doc.ComputeStatistics(wdStatisticWords) & Chr(13)
Info = Info & "Characters: " & Doc.ComputeStatistics(wdStatisticCharacters)
GetDocumentInfo = Info
End Function
' Replace placeholders like {CUSTOMER_NAME} with actual data
Function ReplacePlaceholders(Placeholders)
Dim Doc, i, Parts, Key, Value, Count
Set Doc = Application.ActiveDocument : Count = 0
For i = LBound(Placeholders) To UBound(Placeholders)
Parts = Split(Placeholders(i), "=", 2)
If UBound(Parts) >= 1 Then
Key = "{" & Trim(Parts(0)) & "}" : Value = Parts(1)
With Doc.Content.Find
.Text = Key : .Replacement.Text = Value
.Forward = True : .Wrap = 1 : .Execute(Replace:=2)
End With
Count = Count + 1
End If
Next i
ReplacePlaceholders = Count
End Function
' Export the active document to PDF
Function ExportToPDF(PDFPath)
On Error Resume Next
Application.ActiveDocument.ExportAsFixedFormat _
OutputFileName:=PDFPath, ExportFormat:=17, OpenAfterExport:=False
ExportToPDF = (Err.Number = 0)
End Function
' Insert text with specific formatting
Sub InsertFormattedText(Text, Style)
With Application.Selection
Select Case Style
Case "Bold": .Font.Bold = True
Case "Italic": .Font.Italic = True
Case Else: .Font.Bold = False: .Font.Italic = False
End Select
.TypeText Text:=Text
End With
End Sub

Build the protected DLL that will replace your source code.

  1. Click “Publish Final DLL” in the ribbon.

  2. Click “Build Final DLL Files”.

  3. VBA Padlock produces three DLLs in a bin subfolder next to your document:

    • DocumentBuilderrun32.dll (32-bit runtime)
    • DocumentBuilderrun64.dll (64-bit runtime)
    • DocumentBuilder.dll (Your compiled code)

The VBA Bridge is the connection between your Word document and the DLL.

  1. Open your Word file in Microsoft Word.

  2. In VBA Padlock, click Create VBA BridgeInject Into Office.

  3. VBA Padlock adds the VBADLLBridge module to your Word project.

    VBA Bridge in Word


Now you can call your compiled functions from regular Word VBA code using VBAPL_Execute.

Word VBA Editor

In the Word VBA Editor (Alt+F11), create a new module and add this:

'---------------------------------------------------------------
' Simple hello world
'---------------------------------------------------------------
Sub Demo_HelloWorld()
Call VBAPL_Execute("HelloWorld")
End Sub
'---------------------------------------------------------------
' Show document information
'---------------------------------------------------------------
Sub Demo_DocumentInfo()
Dim Info As Variant
' Call the compiled DLL function
Info = VBAPL_Execute("GetDocumentInfo")
MsgBox Info, vbInformation, "Document Info"
End Sub
'---------------------------------------------------------------
' Insert formatted text at cursor
'---------------------------------------------------------------
Sub Demo_InsertFormattedText()
' Insert bold text
Call VBAPL_Execute("InsertFormattedText", "This is bold text. ", "Bold")
' Insert italic text
Call VBAPL_Execute("InsertFormattedText", "This is italic text. ", "Italic")
' Insert normal text
Call VBAPL_Execute("InsertFormattedText", "This is normal text.", "Normal")
End Sub

Generated Word Content


To require an activation key before your document can be used:

  1. Go to Licensing FeaturesActivation Settings.

  2. Check Activation key is required.

  3. Optional: Enable Hardware Locking to tie licenses to specific PCs.

    Activation Settings

  4. Rebuild the DLL to apply the security settings.

  5. Generate a test key in the Key Generator.

    Key Generator


Package your application for release.

  1. Click Distribute in the ribbon.

  2. Select Create ZIP Archive.

  3. The archive will contain your .docm file and the bin folder (mandatory).

    Distribution Tab