将工作表复制到另一个excel实例中的另一个工作簿VB

时间:2017-09-12 01:12:43

标签: excel vb.net

我需要简单地将工作表从一个工作簿复制到另一个excel实例中的另一个工作簿。在使用下面的代码时,我在“inputsht.Copy(After:= chartheet)”行中出现错误('HRESULT异常:0x800A03EC'),有人可以告诉我有什么问题。(excelWB已定义为public var。)

Private Sub ChartInExcelBtn_Click(sender As Object, e As RoutedEventArgs) Handles ChartInExcelBtn.Click

    Dim excelWB As Excel.Workbook
    Dim inputsht As Worksheet
    Dim Chartfile As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim chartworkbook As Excel.Workbook
    Dim chartsheet As Excel.Worksheet


    excelWB = System.Runtime.InteropServices.Marshal.BindToMoniker(xlfile)
    inputsht = excelWB.Worksheets("input")


    chartworkbook = Chartfile.Workbooks.Add
    chartsheet = chartworkbook.Worksheets.Add

    'inputsht.Copy(After:=chartworkbook.Worksheets("Sheet1"))
    inputsht.Copy(After:=chartsheet)

    Chartfile.Visible = True


End Sub

1 个答案:

答案 0 :(得分:0)

我在下面的链接中找到了答案:Copy Worksheet from one instance of Excel to another

所以我改变了下面的代码,它正在运行:

Private Sub ChartInExcelBtn_Click(sender As Object, e As RoutedEventArgs) Handles ChartInExcelBtn.Click
    Dim excelWB As Excel.Workbook
    Dim inputsht As Worksheet
    Dim Chartfile As Excel.Application = New Microsoft.Office.Interop.Excel.Application()


    excelWB = System.Runtime.InteropServices.Marshal.BindToMoniker(xlfile)
    inputsht = excelWB.Worksheets("input")
    inputsht.Copy(Type.Missing, Type.Missing)
    excelApp.DisplayAlerts = False
    excelApp.Workbooks(2).SaveAs("c:\test.xlsx")

    Chartfile.Workbooks.Open("c:\test.xlsx", Notify:=False, ReadOnly:=True)
    Chartfile.Visible = True
    excelApp.Workbooks(2).Close(SaveChanges:=False)



End Sub