在 Excel.Application 的不同实例之间复制工作表

Copying worksheets between different instance of Excel.Application

我正在开发一个 VSTO Excel 加载项,在某些时候,它会从 Resources 打开一个模板工作簿并将 sheet 从它复制到 运行 实例Excel。我想在从模板复制时避免短的白色 window 闪烁,所以我创建了一个 Excel.Application 的隐藏实例并从那里调用它。这部分有效,但在复制时,我不断收到 "System.Runtime.InteropServices.COMException: 'Copy method of Worksheet class failed'"

Dim tempFileName As String = "DesignWorks1_Template"
Dim tempName As String = Path.GetTempPath() & "\" & tempFileName & ".xlsx"
Dim ResMgr = New Resources.ResourceManager("MyUtilities.Resources", System.Reflection.Assembly.GetExecutingAssembly)
Dim fstream As New FileStream(tempName, FileMode.Create, FileAccess.Write)
Dim filestreamWrite As New BinaryWriter(fstream)
filestreamWrite.Write(My.Resources.DesignWorks1, 0, My.Resources.DesignWorks1.Length)
fstream.Close()
filestreamWrite.Close()

Dim currentWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim newHiddenApp As New Excel.Application
newHiddenApp.Visible = False
Dim oTemplate As Excel.Workbook = newHiddenApp.Workbooks.Add(tempName)
oTemplate.Worksheets(compareName).Copy(currentWorkbook.Worksheets(1)) 'error here
oTemplate.Close()
My.Computer.FileSystem.DeleteFile(tempName)
ResMgr.ReleaseAllResources()

提前致谢。

当运行在桌面上Excel时,无法在实例之间复制工作表。因此,我怀疑当 Excel 实例以 VB.

启动时是否可以完成

为了在工作簿之间复制工作表,这些工作簿需要在同一实例中运行。

Is it possible to specify a particular sheet from the template? I tried Sheets.Add but it seems to import all worksheets from the specified path. – Virtual Underscore

这是将工作簿中的每个 sheet 导出到 WorkSheet 模板文件的主要难点。当您执行另存为 Worksheet 模板时,主题 Worksheet 必须是工作簿中唯一的 Worksheet。如果工作簿中存在多个工作sheet,您将导出一个工作簿模板。

您可以将以下 VBA 代码复制到您要从中导出模板的工作簿的 ThisWorkbook 代码文件中。确保修改代码中的 templateFolder = "F:\Temp\Templates" 行。 运行 它将导出工作簿中的每个工作sheet。

Sub ExportWorksheetTemplates()
   Excel.Application.DisplayAlerts = False
   Excel.Application.ScreenUpdating = False

   Dim templateFolder As String
   templateFolder = "F:\Temp\Templates"   ' set this to an existing folder on you system

   Dim tmpWb As Excel.Workbook
   Dim old_numsheets As Integer

   old_numsheets = Excel.Application.SheetsInNewWorkbook
   Excel.Application.SheetsInNewWorkbook = 1

   Dim ws As Excel.Worksheet
   Dim newWBFirstSheet As Excel.Worksheet

   Dim templatePath As String

   On Error GoTo Finalize

   For Each ws In ThisWorkbook.Worksheets
      Set tmpWb = Excel.Application.Workbooks.Add()
      Set newWBFirstSheet = tmpWb.Worksheets(1)
      newWBFirstSheet.Name = "."
      ws.Copy after:=newWBFirstSheet
      newWBFirstSheet.Delete


      templatePath = templateFolder & "\" & tmpWb.Worksheets(1).Name & ".xltx"
      tmpWb.Worksheets(1).SaveAs templatePath, Excel.XlFileFormat.xlOpenXMLTemplate

      tmpWb.Close
   Next

Finalize:
   Excel.Application.DisplayAlerts = True
   Excel.Application.ScreenUpdating = True
   Excel.Application.SheetsInNewWorkbook = old_numsheets
End Sub

现在你可以将这些文件中的每一个添加到你的 VSTO 项目的资源中。

然后您可以像当前一样将资源模板导出到临时文件。

当您使用 Sheets.Add method 并指定 Type:="path to template" 时,您现在只会将单个作品sheet 添加到工作簿中。