Walkthrough: Creating a New Office Project Using Visual Studio Project Automation
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
This walkthrough demonstrates how to create a macro that uses the Visual Studio object model to automate the creation of a Visual Studio Tools for Office project. The project uses custom settings in the Visual Studio Tools for Office Project Wizard, instead of relying on project defaults.
The code creates a new project with custom wizard settings so that the automated project is created using an existing Microsoft Office Excel workbook instead of creating a new workbook.
For more information about macros, see Automating Repetitive Actions by Using Macros.
This walkthrough illustrates the following tasks:
Creating a new Visual Studio macro.
Creating a temporary template file (.vstemplate) with custom settings.
Setting parameters for a wizard and starting the wizard.
Prerequisites
You need the following components to complete this walkthrough:
Visual Studio.
Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).
Microsoft Office Excel 2003.
Creating the Excel Workbook
You must create an Excel workbook that is used as the existing workbook in the new project.
To create the Excel workbook
Create a folder on the computer's C drive called CreateNewFile.
Create a subfolder in CreateNewFile called Test: C:\CreateNewFile\Test.
Open Microsoft Office Excel 2003.
Type This is the CreateNewFile workbook in cell A1.
Save the workbook as CreateNewFile.xls in the location C:\CreateNewFile.
Close Excel.
The Test subfolder is used as the location for the completed project.
Creating a New Macro
In this step, you will create a new, empty Visual Studio macro.
To create a new macro
Open Visual Studio and close any open projects.
On the Tools menu, point to Macros, and then click Macros IDE.
The Microsoft Visual Studio Macros integrated development environment (IDE) opens.
Expand the MyMacros node, if it is not already expanded.
On the Project menu, click Add Module.
Name the module CreateNewProject, and then click Add.
The new module opens in the Macros IDE. Now you can add code to create a new project.
Creating a Template File (.vstemplate)
To create a new project using default wizard settings, you use one of the pre-defined template files. In Visual Studio Tools for Office, the project default is to create a new document or workbook for use in the project. To use an existing workbook in your project, you must generate a custom temporary .vstemplate file with the correct settings.
For more information about .vstemplate files, see Visual Studio Templates.
To use an existing document, add the custom parameter VSTOExistingDocumentPath to the .vstemplate file.
To generate a temporary .vsz file
On the Project menu, click Add Reference.
Select System.Xml.dll, click Add, and then click OK.
Add the following Imports statements at the top of the code file.
Imports System.Xml Imports System.IO
In the module CreateNewProject, add the following method to define the contents of the temporary template folder.
Sub CreateVstemplateFile(ByVal defaultTemplateFilePath As String, _ ByVal templateFilePath As String, ByVal docFilePath As String) ' Copy the default template in the temporary location. Dim defaultTemplateDirectory = _ Path.GetDirectoryName(defaultTemplateFilePath) Dim templateDirectory = Path.GetDirectoryName(templateFilePath) If Not Directory.Exists(templateDirectory) Then Directory.CreateDirectory(templateDirectory) End If For Each fileToCopy As String In Directory. _ GetFiles(defaultTemplateDirectory) File.Copy(fileToCopy, Path.Combine(templateDirectory, _ Path.GetFileName(fileToCopy)), True) Next ' Create the vstemplate namespace. Dim vstemplateNamespace As String = _ "https://schemas.microsoft.com/developer/vstemplate/2005" ' Load the XML document. Dim doc As XmlDocument = New XmlDocument() doc.Load(templateFilePath) Dim xmlNsManager As XmlNamespaceManager = _ New XmlNamespaceManager(doc.NameTable) xmlNsManager.AddNamespace("vstemplns", vstemplateNamespace) ' Get 'CustomParameters' XML node. Dim customParametersNode As XmlNode = doc.SelectSingleNode( _ "/vstemplns:VSTemplate/vstemplns:TemplateContent/" _ & "vstemplns:CustomParameters", xmlNsManager) ' Create a new XML CustomParameter node with ' the existing document data. ' <CustomParameter Name="VSTOExistingDocumentPath" _ ' Value="C:\CreateNewFile\CreateNewFile.xls" /> Dim newNode As XmlNode newNode = doc.CreateElement("CustomParameter", _ vstemplateNamespace) Dim nameAttribute As XmlAttribute nameAttribute = doc.CreateAttribute("Name") Dim valueAttribute As XmlAttribute valueAttribute = doc.CreateAttribute("Value") nameAttribute.Value = "VSTOExistingDocumentPath" valueAttribute.Value = docFilePath newNode.Attributes.Append(nameAttribute) newNode.Attributes.Append(valueAttribute) customParametersNode.AppendChild(newNode) doc.Save(templateFilePath) End Sub
The next method defines parameters and calls the LaunchWizard method.
Starting the Wizard
To create a new project, use the LaunchWizard method of the DTE object.
To launch the wizard
Add the following method to the CreateNewProject module to fill the parameter array and start the wizard:
Sub LaunchWizard(ByVal projectName As String, _ ByVal projectFolder As String, _ ByVal templateFilePath As String) Dim params(6) As Object ' New project. params(0) = "{0F90E1D0-4999-11D1-B6D1-00A0C90F2744}" ' Project name. params(1) = projectName ' Project location. params(2) = projectFolder ' Install location. params(3) = "" ' Close solution. params(4) = False ' Solution name. params(5) = "" ' Silent - do not display any user interface. params(6) = False DTE.LaunchWizard(templateFilePath, params) End Sub
Finally, add a method to call the two methods you just created and pass in the appropriate parameters.
Creating the Project
The following method defines and passes the correct parameters for the CreateVstemplateFile and LaunchWizard methods.
To create the project
Add the following method to the CreateNewProject module:
Sub CreateProject() Dim sol As Solution2 = DTE.Solution ' Get the path of the default .vstemplate file using ' the name of the template zip file and the language. ' The zip files are "VSTOExcelWorkbook.zip", ' "VSTOExcelTemplate.zip", ' "VSTOWordDocument.zip", ' or "VSTOWordTemplate.zip". ' The languages are "VisualBasic" and "CSharp". Dim defaultTemplateFilePath As String = _ sol.GetProjectTemplate("VSTOExcel2003Workbook.zip", _ "VisualBasic") ' Get the temporary .vstemplate file containing ' the specification. Dim templateDirectory As String = "C:\CreateNewFile\template" Dim templateFilePath = Path.Combine(templateDirectory, _ Path.GetFileName(defaultTemplateFilePath)) ' Get an existing document to use in project creation. Dim docFilePath As String = "C:\CreateNewFile\CreateNewFile.xls" ' Create a temporary template with the wizard specification. CreateVstemplateFile(defaultTemplateFilePath, _ templateFilePath, docFilePath) ' Launch the CreateProject wizard. Dim projectName As String = "CreateNewFile" Dim projectFolder As String = "c:\CreateNewFile\test" LaunchWizard(projectName, projectFolder, templateFilePath) End Sub
Save the macro.
Close the macros IDE.
Testing the Macro
Now you can test your macro to make sure that it creates a new project.
To test the macro
On the Tools menu, point to Macros, and then click Macro Explorer.
In Macro Explorer, under MyMacros, expand the CreateNewProject macro.
Under CreateNewProject, double-click CreateProject.
The Visual Studio Tools for Office Project Wizard opens.
In the Select a Document For Your Application page, click OK.
Verify that the new project is created in the Test subfolder.
See Also
Tasks
How to: Add Worksheets to Workbooks Using Visual Studio Project Automation
How to: Change Excel Properties Using Visual Studio Project Automation
Concepts
Visual Studio Tools for Office Project Extensibility Overview
Visual Basic and Visual C# Project Extensibility Examples