复制时,VB.net HRESULT:0x800A03EC(excel)

时间:2018-01-09 22:11:46

标签: excel vb.net visual-studio

我试图将数据从excel工作簿复制到另一个工作簿,但我在尝试复制时遇到HRESULT:0x800A03EC。

我已经详细阅读并在之前的帖子HRESULT: 0x800A03EC on Worksheet.range

中尝试了所有内容

除了在DCOM配置中更改excel应用程序的属性之外的所有内容(我没有看到excel应用程序)

下面是代码,错误以粗体显示。

    Imports Excel = Microsoft.Office.Interop.Excel
    Dim xlApp As Excel.Application = Nothing
    Dim newxlApp As Excel.Application = Nothing
    Dim xlWorkBooks As Excel.Workbooks = Nothing
    Dim xlWorkBook As Excel.Workbook = Nothing
    Dim xlWorkSheet As Excel.Worksheet = Nothing
    Dim newxlworkbooks As Excel.Workbook = Nothing
    Dim newxlworkbook As Excel.Workbook = Nothing
    Dim newxlworksheet As Excel.Worksheet = Nothing

    Dim String2Search4 As String = "1000"  'String to search for.
    Dim ColumnNumber As Integer = 4  

    xlApp = CreateObject("Excel.application")
    newxlApp = CreateObject("Excel.application")
    xlWorkBook = xlApp.Workbooks.Open("C:\spreadseet1.xlsx")
    newxlworkbook = newxlApp.Workbooks.Open("C:\spreedsheet2.xlsx")
    newxlworksheet = newxlworkbook.Worksheets("Sheet1")
    xlWorkSheet = xlWorkBook.Worksheets("Sheet1")

    For X As Integer = 1 To xlWorkSheet.Rows.Count Step 1
        'check if the cell value matches the search string.
        If xlWorkSheet.Cells(X, ColumnNumber).value = String2Search4 Then
            MsgBox("Found " & String2Search4 & " in row " & X)
            Dim rowfound As String = (X.ToString + ":" + X.ToString)
            **xlWorkSheet.Copy(xlWorkSheet.Cells(2, 2), newxlworksheet.Cells(1, 1))**
            newxlworkbook.Close()
            xlWorkBook.Close()
        End If
    Next

- 由于

1 个答案:

答案 0 :(得分:1)

您的复制语法不正确:

xlWorkSheet.Cells(2, 2).Copy(newxlworksheet.Cells(1, 1))