Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office 2003

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

In this walkthrough, you will create 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.

During this walkthrough, you will learn how to:

  • Create a new Visual Studio macro.

  • Create a temporary template file (.vstemplate) with custom settings.

  • Set parameters for a wizard and launch the wizard.

To complete this walkthrough, you will need:

  • Visual Studio

  • Visual Studio Tools for Office

  • Microsoft Office Excel 2003

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 Microsoft Office Excel 2003.

  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.

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. Double-click the MyMacros node to expand it, 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.

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

  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 = _
            "http://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.

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

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("VSTOExcelWorkbook.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.

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.

  4. Verify that the new project is created in the Test subfolder.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.