打开多个实例时更改ActiveWorkbook

时间:2014-03-05 14:54:26

标签: c# excel vsto

我正在打开一个Excel文件,如果它不符合某些条件,我就会创建一个新的工作簿。我遇到的问题是,在创建新工作簿时,默认设置为activeworkbook,因此当我尝试使用activeworkbook.saveas()时,会保存第一个工作簿,但数据不准确。

这是我用来创建新工作簿的内容:

Excel.Workbook newWorkbook = this.Application.Workbooks.Add();

有人可以告诉我如何在打开多个工作簿时在Excel中更改activeworkbook吗?

BTW这是我正在使用的Excel 2007。

将工作表复制到新工作簿

ws = wb.Sheets["Mitchell"];
((Excel.Worksheet)ws).Copy();

2 个答案:

答案 0 :(得分:1)

只需在要显示的工作簿上调用Activate()方法即可。

创建新工作簿时,您可以使用:

Excel.Wokrbook oldWorkbook = this.Application.ActiveWorkbook;

Excel.Workbook newWorkbook = this.Application.Workbooks.Add();

oldWorkbook.Activate();

过了一会儿:

newWorkbook.Activate();

请参阅MSDN上的docs

除了:

  

Workbooks集合的Activate方法激活Microsoft Office Excel工作簿并选择工作簿中的第一个工作表。

修改

这对我有用:

Excel.Workbook oldWorkbook = this.Application.ActiveWorkbook;

this.Application.ActiveWorkbook.Sheets["name"].Copy();

Excel.Workbook newWorkbook = this.Application.ActiveWorkbook;

old = Book1

new = Book2

答案 1 :(得分:1)

配置详细信息:

  • 使用Visual Studio 2010 Professional

  • 在vb.net中编写代码
  • 所有示例代码都放在
    ThisAddIn_Startup事件处理程序中 Excel 2010的应用程序级加载项

  • 在项目参考中(右键单击项目>属性>
    参考)确保Excel = Microsoft.Office.Interop.Excel

  • 旁边有一张支票

当多个工作表打开时,我使用For Each遍历集合并查看名称...

    For Each wrkbk As Excel.Workbook In Me.Application.Workbooks

        MsgBox(wrkbk.Name)

    Next

将其与Select Case结合使用,这样您就可以获得任何已打开的工作簿。

    For Each wrkbk As Excel.Workbook In Me.Application.Workbooks

        Select Case wrkbk.Name

            Case "Book1"

                wrkbk.Activate()

            Case "Book2"


            Case Else


         End Select

     Next

现在,将它们捆绑在一起,你可以做到这一点:

  1. 创建名为 CustomWorkbook.xlsx
  2. 的新工作簿
  3. 激活 Book1 ,复制 Sheet1 ,然后将其粘贴到CustomWorkbook.xlsx中(将工作表重命名为 CopiedWorksheet
  4. 激活CustomWorkbook.xlxs

    //Hide alerts so no prompts display when using SaveAs to rename new workbook
    Me.Application.DisplayAlerts = False
    
    //Programatically create a new workbook
    Dim myWrkbk As Excel.Workbook
    myWrkbk = Me.Application.Workbooks.Add()
    
    //Name new workbook
    myWrkbk.SaveAs("CustomWorkbook")
    
    //Set Book1 as the active workbook (so can copy sheet)
    Application.Workbooks("Book1").Activate()
    
    //Create a new worksheet variable
    Dim sourceWrksht As Excel.Worksheet
    sourceWrksht = Application.Worksheets("Sheet1")
    sourceWrksht.Range("A1").Value = "Text in a Cell"
    
    //Select Sheet1 and copy into CustomWorkbook and paste as first sheet
    sourceWrksht.Select()
    sourceWrksht.Copy(Before:=Application.Workbooks("CustomWorkbook.xlsx").Sheets(1))
    
    //Rename copied worksheet
    Dim destinationWrksht As Excel.Worksheet
    destinationWrksht = Application.Workbooks("CustomWorkbook.xlsx").Sheets(1)
    
    destinationWrksht.Name = "CopiedWorksheet"
    
    //Loop through all open workbooks in Excel Application
    For Each wrkbk As Excel.Workbook In Me.Application.Workbooks
    
    
        Select Case wrkbk.Name
    
            Case "CustomWorkbook.xlsx"
    
                wrkbk.Activate()
    
            Case "Book1"
    
    
            Case Else
    
        End Select
    
    Next
    
    //Set Excel to show alerts again
    Me.Application.DisplayAlerts = True