Overview:
Finding DLL’s programmatically can be challenging if they are not well organized. For VSTA integration several DLL’s may need to be found including add-in’s to load. In the ShapeApp samples included with the SDK, the add-in samples copy their DLL’s into the folders %MyDocuments%\%HostID%\AppAddins and %MyDocuments%\%HostID%\DocAddins and only these folders are searched for add-ins to load. How these DLL’s are moved is not explained in the walkthrough, but is fairly straightforward. In the project Properties, a Post build event command line command is specified. This command runs a file, InstallAddIn.js, which is included here as well as in the ShapeAppBasic and ShapeAppAdvanced samples. The file is very simple- only 11 lines; it copies the DLL to a subfolder of %My Documents% which is specified in the command. The actual command used is:
|
cscript "$(ProjectDir)InstallAddIn.js" "$(TargetPath)" "HostID\Subfolder\$(TargetName)" |
To use this for your own project be sure to update the HostId and Subfolder. The InstallAddIn.js file needs to be copied to the same location as the project (the .vbproj or .csproj file). If the file specified to store the dll’s in does not exist, the script creates it.
Step-by-Step: How to update a project so that the DLL is copied to %MyDocuments%\<HostId>\Addins.
1) Create and build the project.
2) Add the InstallAddIn.js file to the project. Once the file is added it will be moved to the correct location (which is in the same folder as the project .csproj or .vbproj file).
· In the Solution Explorer right click on the project and select Add then Existing Item.
· Navigate to the file InstallAddIn.js pasted above.
· Ensure that the filter Files of type is set to “All Files”.
· Select the file InstallAddIn.js then hit Add to add the file to the project.
3) Open the Properties page of the project.
· For a Visual C# project:
i. Open the project in Visual Studio 2005 or Visual Studio 2005 Tools for Applications.
ii. In the Solution Explorer right click on the project and select Properties.
iii. Select the Build Events tab.
iv. In the Post-build event command line box enter the command above updated as needed.
v. Ensure that the Run the post-build event drop down box is set to “On successful build”.
vi. Build the project.
· For a Visual Basic project:
i. Open the project in Visual Studio 2005 NOT Visual Studio 2005 Tools for Applications.
ii. In the Solution Explorer right click on the project and select Properties.
iii. Select the Compile tab.
iv. Select the Build Events button towards the bottom of the page. This button is not present in Visual Studio 2005 Tools for Applications- only Visual Studio 2005.
v. The Build Events window should now appear. In the Post-build event command line box, enter the command above updated as needed.
vi. Ensure that the Run the post-build event drop down box is set to “On successful build”.
vii. Select OK.
viii. Build the project
4) Error Codes: If the build fails, here are some common errors I encountered.
“The command … exited with error code”:
1- The file IntallAddIn.js was not copied to the correct location. Ensure that the file is in the same folder as the project (.csproj or .vbproj file).
3- The command in the Post-build event command line box may be incorrect. Check that it is all on one line.
5) The folder specified in the command line (for example if you used <HostId>\Addins, %MyDocuments%\<HostId>\AddIns ) should now appear with a subfolder for the project updated above. The DLL for the project will be located in this subfolder.
Continue to Part II: Project Templates
Part III: Supporting Projects
Part IV: Keeping the Current Proxy in the GAC