There are several different copies of the document where you can add VBA code that calls into the Visual Studio Tools for Office customization.
As you develop and test your solution, you can write VBA code in the document that opens while you debug or run your project in Visual Studio (that is, the document in the build output folder). However, any VBA code you add to this document will be overwritten the next time that you build the project, because Visual Studio replaces the document in the build output folder with a copy of the document from the main project folder.
If you want to save the VBA code that you add to the document while debugging or running the solution, copy the VBA code into the document in the project folder. For more information about the build process, see Office Solution Build Process Overview.
When you are ready to deploy your solution, there are three main document locations in which you can add the VBA code.
In the Project Folder on the Development Computer
This location is convenient if you have complete control over both the VBA code in the document and the Visual Studio Tools for Office customization code. Because the document is on the development computer, you can easily modify the VBA code if you change the customization code. VBA code that you add to this copy of the document remains in the document when you build, debug, and publish your solution.
You cannot add the VBA code to the document while it is open in the designer. You must first close the document in the designer, and then open the document directly in Word or Excel.
Caution: |
|---|
If you add VBA code that runs when the document is opened, in rare cases this code might corrupt the document or prevent it from opening in the designer. |
In the Publish or Installation Folder
In some cases, it might be suitable to add the VBA code to the document in the publish or installation folder. For example, you might choose this option if the VBA code is written and tested by a different developer on a computer that does not have Visual Studio Tools for Office installed.
If users install the solution directly from the publish folder, you must add the VBA code to the document every time you publish the solution. Visual Studio overwrites the document in the publish location when you publish the solution.
If users install the solution from an installation folder that is different from the publish folder, you can avoid adding the VBA code in the document every time you publish the solution. When a publish update is ready to be moved from the publish folder to the installation folder, copy all of the files to the installation folder except for the document.
On the End User Computer
If the end users are VBA developers who are calling into services that you provide in the Visual Studio Tools for Office customization, you can tell them how to call your code by using the CallVSTOAssembly property or the GetManagedClass method in their copies of the document. When you publish updates to the solution, VBA code in the document on the end user computer will not be overwritten, because the document is not modified by publish updates.