Walkthrough: Creating a New Office Project Using Visual Studio Project Automation

This walkthrough demonstrates how to create a macro that uses the Visual Studio object model to automate the creation of an Office project. The project uses custom settings in the Visual Studio Tools for Office Project Wizard, instead of relying on project defaults.

Applies to: The information in this topic applies to document-level projects for Excel 2010. For more information, see Features Available by Office Application and Project Type.

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:

-

An edition of Visual Studio 2010 that includes the Microsoft Office developer tools. For more information, see [Configuring a Computer to Develop Office Solutions](bb398242\(v=vs.100\).md).
  • Excel 2010.

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

  1. Create a folder on the computer's C drive called CreateNewFile.

  2. Create a subfolder in CreateNewFile called Test: C:\CreateNewFile\Test.

  3. Open Excel 2010.

  4. Type This is the CreateNewFile workbook in cell A1.

  5. Save the workbook as CreateNewFile.xls in the location C:\CreateNewFile.

  6. 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

  1. Open Visual Studio and close any open projects.

  2. On the Tools menu, point to Macros, and then click Macros IDE.

    The Microsoft Visual Studio Macros integrated development environment (IDE) opens.

  3. Expand the MyMacros node, if it is not already expanded.

  4. On the Project menu, click Add Module.

  5. 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, 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

  1. On the Project menu, click Add Reference.

  2. Select System.Xml.dll, click Add, and then click OK.

  3. Add the following Imports statements at the top of the code file.

    Imports System.Xml
    Imports System.IO
    
  4. 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

  1. 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("VSTOExcel2010WorkbookV4.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
    
  2. Save the macro.

  3. 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

  1. On the Tools menu, point to Macros, and then click Macro Explorer.

  2. In Macro Explorer, under MyMacros, expand the CreateNewProject macro.

  3. Under CreateNewProject, double-click CreateProject.

    The Visual Studio Tools for Office Project Wizard opens.

  4. In the Select a Document For Your Application page, click OK.

  5. 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 Basic and Visual C# Project Extensibility Examples

Other Resources

Extensibility in Office Projects