Sign VBA macros with Digital signature

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Signature MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-signature -- dnx GroupDocs.Signature.Mcp --yes
Folders, licensing and more →

GroupDocs.Signature provides DigitalSignOptions class to specify different amount of settings for Digital signature

  • Class DigitalVBA allows to configure certificate settings for macros signing

VBA projects are signed in macro-enabled spreadsheets, such as .xlsm files.

Here are the steps to add Digital signature into document with GroupDocs.Signature:

  • Create new instance of Signature class and pass source Excel document path as a constructor parameter.
  • Instantiate the DigitalSignOptions object
  • Instantiate the DigitalVBA object with certificate path and password, and add it to the list returned by getExtensions() of the options.
  • Call sign method of Signature class instance and pass DigitalSignOptions to it.

This example shows how to sign only the VBA project of the document. See SignResult

Signature signature = new Signature(excelFilePath);
try
{
    // create digital signature options without a digital certificate
    DigitalSignOptions signOptions = new DigitalSignOptions();
    // add extension for signing VBA project digitally
    DigitalVBA digitalVBA = new DigitalVBA(certificatePath, password);
    // set to true only for signing VBA project
    digitalVBA.setSignOnlyVBAProject(true);
    digitalVBA.setComments("VBA Comment");
    signOptions.getExtensions().add(digitalVBA);

    // sign document to file
    SignResult signResult = signature.sign(outputFilePath, signOptions);
}
finally
{
    signature.dispose();
}

To sign both the spreadsheet and its VBA project, create the options with the certificate and do not call setSignOnlyVBAProject(true):

Signature signature = new Signature(excelFilePath);
try
{
    // digital signature options for the document itself
    DigitalSignOptions signOptions = new DigitalSignOptions(certificatePath);
    signOptions.setPassword(password);
    signOptions.getSignature().setComments("Comment");

    // add extension for signing VBA project digitally
    DigitalVBA digitalVBA = new DigitalVBA(certificatePath, password);
    digitalVBA.setComments("VBA Comment");
    signOptions.getExtensions().add(digitalVBA);

    // sign document to file
    SignResult signResult = signature.sign(outputFilePath, signOptions);
}
finally
{
    signature.dispose();
}

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured Java library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.