vsto - VB - 从 outlook 插件中的列中查找最后一个单元格

vsto - VB - find last cell from column in outlook addin

如何从 vsto outlook 插件中搜索 excel sheet 中的最后一个空单元格?

我有以下代码(未编译)

Imports Excel = Microsoft.Office.Interop.Excel
Dim ExcelApp As New Excel.Application
Dim ExcelWorkbook As Excel.Workbook
Dim ExcelWorkSheet As Excel.Worksheet= ExcelWorkbook.Worksheets(1)
Dim ExcelRange As Excel.Range = ExcelWorkSheet.Range("A1","A600")

Dim currentFind As Excel.Range = Nothing
Dim firstFind As Excel.Range = Nothing

currentFind = ExcelRange.Find("*", , Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)

            While Not currentFind Is Nothing

                ' Keep track of the first range you find.
                If firstFind Is Nothing Then
                    firstFind = currentFind

                    ' If you didn't move to a new range, you are done.
                ElseIf currentFind.Address = firstFind.Address Then
                    Exit While
                End If

                currentFind = ExcelRange.FindNext(currentFind)
            End While

ExcelWorkbook.ActiveSheet.range(currentFind).Select()

我已经根据 Scott Holtzman 的评论对其进行了更新,但现在我收到一条错误消息:HRESULT: 0x800A03EC

根据对象模型,代码没有正确的层次结构。

如果不先定义 Worksheet 对象,就无法定义 Range 对象,而在定义之前需要 Workbook 对象。

试试这个:

Set ExcelApp = New Excel.Application

Dim ExcelWorkbook as Excel.Workbook
Set ExcelWorkbook = ExcelApp.Workbooks.Open("myPath") 'actually opens a workbook to work with 

Dim ExcelWorksheet as Excel.Worksheet
Set ExcelWorksheet = ExcelWorkbook.Worksheets("mySheet")

Dim currentFind As Excel.Range = Nothing
Dim firstFind As Excel.Range = Nothing

Dim Fruits As Excel.Range = ExcelWorksheet.Range("A1", "A200")
Set currentFind = Fruits.Find("apples", , Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)


... 


Set currentFind = Fruits.FindNext(currentFind)

已解决:我有以下代码(正在编译!)

Imports Excel = Microsoft.Office.Interop.Excel
Dim ExcelApp As New Excel.Application
Dim ExcelWorkbook As Excel.Workbook
Dim ExcelWorkSheet As Excel.Worksheet= ExcelWorkbook.Worksheets(1)

Dim LastRow As Integer

LastRow = ExcelWorkSheet.Columns(1).Find("*", , , , Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious).Row

ExcelWorkSheet.Range("A" & LastRow).Select()

我的错误在于实际的 属性 库选择。谨慎选择: XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious