演练:使用 Visual Studio 项目自动化新建 Office 项目

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

Microsoft Office 版本

  • Excel 2003

有关更多信息,请参见按应用程序和项目类型提供的功能

此演练演示如何创建使用 Visual Studio 对象模型使 Visual Studio Tools for Office 项目的创建过程实现自动化的宏。项目使用“Visual Studio Tools for Office 项目向导”中的自定义设置,而不依赖项目的默认设置。

代码使用自定义向导设置创建新项目,以便使用现有的 Microsoft Office Excel 工作簿而非通过创建新工作簿来创建自动化项目。

有关宏的更多信息,请参见 使用宏自动执行重复性操作

本演练阐释以下任务:

  • 创建新的 Visual Studio 宏。

  • 使用自定义设置创建临时模板文件 (.vstemplate)。

  • 设置向导的参数并启动向导。

先决条件

您需要以下组件来完成本演练:

  • Visual Studio。

  • Visual Studio Tools for Office(Visual Studio 2008 专业版 和 Visual Studio Team System 的可选组件)。

  • Microsoft Office Excel 2003。

创建 Excel 工作簿

必须创建一个 Excel 工作簿,将其用作新项目中的现有工作簿。

创建 Excel 工作簿

  1. 在计算机的 C 驱动器上创建一个名为“CreateNewFile”的文件夹。

  2. 在“CreateNewFile”中创建一个名为 Test: C:\CreateNewFile\Test 的子文件夹。

  3. 打开 Microsoft Office Excel 2003。

  4. 在单元格“A1”中键入“这是 CreateNewFile 工作簿”。

  5. 将该工作簿另存为“CreateNewFile.xls”,保存位置为“C:\CreateNewFile”。

  6. 关闭 Excel。

Test 子文件夹用于放置已完成的项目。

创建新的宏

在此步骤中,您将新建一个空的 Visual Studio 宏。

创建新的宏

  1. 打开 Visual Studio 并关闭任何打开的项目。

  2. 在“工具”菜单上指向“宏”,然后单击“宏 IDE”。

    随即打开“Microsoft Visual Studio 宏”集成开发环境 (IDE)。

  3. 如果“MyMacros”节点尚未展开,请将其展开。

  4. 在“项目”菜单上单击“添加模块”。

  5. 将该模块命名为“CreateNewProject”,然后单击“添加”。

新模块随即在宏 IDE 中打开。这时可以添加代码来创建新项目。

创建模板文件 (.vstemplate)

若要使用默认的向导设置创建新项目,请使用预定义的模板文件之一。在 Visual Studio Tools for Office 中,项目默认为创建一个新的文档或工作簿以便在项目中使用。若要在项目中使用现有的工作簿,则必须生成一个具有正确设置的自定义临时 .vstemplate 文件。

有关 .vstemplate 文件的更多信息,请参见 Visual Studio 模板

若要使用现有文档,请向 .vstemplate 文件添加自定义参数 VSTOExistingDocumentPath。

生成临时 .vsz 文件

  1. 在“项目”菜单上单击“添加引用”。

  2. 选择“System.Xml.dll”,单击“添加”,然后单击“确定”。

  3. 将以下 Imports 语句添加到代码文件顶部。

    Imports System.Xml
    Imports System.IO
    
  4. 在 CreateNewProject 模块中添加以下方法,以定义临时模板文件夹的内容。

    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
    

下一个方法定义参数并调用 LaunchWizard 方法。

启动向导

若要创建一个新项目,请使用 DTE 对象的 LaunchWizard 方法。

启动向导

  • 向“CreateNewProject”模块添加以下方法,以填充参数数组并启动向导:

    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
    

最后,添加一个方法以调用刚创建的两个方法,并传入相应参数。

创建项目

以下方法为 CreateVstemplateFile 和 LaunchWizard 方法定义和传递正确参数。

创建项目

  1. 向“CreateNewProject”模块添加以下方法:

    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
    
  2. 保存宏。

  3. 关闭宏 IDE。

测试宏

现在可以测试该宏,以确保它能创建一个新项目。

测试宏

  1. 在“工具”菜单上指向“宏”,然后单击“宏资源管理器”。

  2. 在“宏资源管理器”的“MyMacros”之下,展开“CreateNewProject”宏。

  3. 在“CreateNewProject”之下双击“CreateProject”。

    会打开“Visual Studio Tools for Office 项目向导”。

  4. 在“为应用程序选择文档”页中单击“确定”。

  5. 验证是否在 Test 子文件夹中创建了新项目。

请参见

任务

如何:使用 Visual Studio 项目自动化向工作簿添加工作表

如何:使用 Visual Studio 项目自动化更改 Excel 属性

概念

Visual Studio Tools for Office 项目扩展性概述

Visual Basic 和 Visual C# 项目扩展性示例

其他资源

Visual Studio Tools for Office 中的项目扩展性