How to: Expose code to VBA in a Visual Basic project • • 5 minutes to read • Contributors • • • • • • In this article You can expose code in a Visual Basic project to Visual Basic for Applications (VBA) code if you want the two types of code to interact with each other. Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see. The Visual Basic process is different from the Visual C# process. For more information, see. The process is different for code in a host item class than it is for code in other classes: • • For a related video demonstration, see. Expose code in a host item class To enable VBA code to call Visual Basic code in a host item class, set the EnableVbaCallers property of the host item to True.
May 04, 2018 MS Excel VBA 5 responses on “ Excel – Unlocking an Excel VBA Project ” John May 31, 2018 at 9:32 am. I found this bit of VBA code on the web. I personally don’t understand how it works, but think it’s some kind of memory trick that lets you into protected VBA code without asking for a.
For a walkthrough that demonstrates how to expose a method of a host item class and then call it from VBA, see. For more information about host items, see. To expose code in a host item to VBA • Open or create a document-level Visual Basic project that is based on a Word document, Excel workbook, or Excel template that supports macros, and that already contains VBA code. For more information about the document file formats that support macros, see. Note This feature cannot be used in Word template projects.
• Ensure that VBA code in the document is allowed to run without prompting the user to enable macros. You can trust VBA code to run by adding the location of the Office project to the list of trusted locations in the Trust Center settings for Word or Excel. • Add the property, method, or event that you want to expose to VBA to one of the host item classes in your project, and declare the new member as Public. The name of the class depends on the application: • In a Word project, the host item class is named ThisDocument by default. • In an Excel project, the host item classes are named ThisWorkbook, Sheet1, Sheet2, and Sheet3 by default. • Set the EnableVbaCallers property for the host item to True. This property is available in the Properties window when the host item is open in the designer.
After you set this property, Visual Studio automatically sets the ReferenceAssemblyFromVbaProject property to True. Note If the workbook or document does not already contain VBA code, or if VBA code in the document is not trusted to run, you will receive an error message when you set the EnableVbaCallers property to True. This is because Visual Studio cannot modify the VBA project in the document in this situation. • Click OK in the message that is displayed. This message reminds you that if you add VBA code to the workbook or document while you are running the project from Visual Studio, the VBA code will be lost the next time you build the project.