Class VbaProject

VbaProject class

Represents the VBA project.

public class VbaProject

Properties

NameDescription
CertRawData { get; }Gets certificate raw data if this VBA project is signed.
Encoding { get; set; }Gets and sets the encoding of VBA project.
IslockedForViewing { get; }Indicates whether this VBA project is locked for viewing.
IsProtected { get; }Indicates whether this VBA project is protected.
IsSigned { get; }Indicates whether VBAcode is signed or not.
IsValidSigned { get; }Indicates whether the signature of VBA project is valid or not.
Modules { get; }Gets all VbaModule objects.
Name { get; set; }Gets and sets the name of the VBA project.
References { get; }Gets all references of VBA project.

Methods

NameDescription
Copy(VbaProject)Copy VBA project from other file.
Protect(bool, string)Protects or unprotects this VBA project.
Sign(DigitalSignature)Sign this VBA project by a DigitalSignature
ValidatePassword(string)Validates protection password.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Vba;
    using Aspose.Cells.DigitalSignatures;
    using System;
    using System.Text;
    using System.Security.Cryptography.X509Certificates;

    public class VbaProjectDemo
    {
        public static void VbaProjectExample()
        {
            // Instantiating a Workbook object
            Workbook workbook = new Workbook();

            // Init VBA project
            VbaProject vbaProject = workbook.VbaProject;

            // Setting properties
            vbaProject.Name = "MyVbaProject";
            vbaProject.Encoding = Encoding.UTF8;

            // Checking read-only properties
            bool isSigned = vbaProject.IsSigned;
            bool isProtected = vbaProject.IsProtected;
            bool isLockedForViewing = vbaProject.IslockedForViewing;
            bool isValidSigned = vbaProject.IsValidSigned;

            // Adding a VBA module
            int moduleIndex = vbaProject.Modules.Add(VbaModuleType.Class, "MyModule");

            // Adding a VBA project reference
            vbaProject.References.AddRegisteredReference("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");

            // Protecting the VBA project
            vbaProject.Protect(true, "password");

            // Signing the VBA project
            X509Certificate2 certificate = new X509Certificate2("path_to_certificate.pfx", "certificate_password");
            DigitalSignature digitalSignature = new DigitalSignature(certificate, "Signature Comments", DateTime.Now);
            vbaProject.Sign(digitalSignature);

            // Saving the Excel file
            workbook.Save("VbaProjectExample.xlsm");

            return;
        }
    }
}

See Also