vsto - VB - 在Outlook插件中查找列中的最后一个单元格
如何从vsto outlook插件搜索excel表单中的最后一个空单元格?vsto - VB - 在Outlook插件中查找列中的最后一个单元格
我有以下代码(未编译)
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()
我已经更新它根据斯科特·霍尔茨曼的意见,但现在我得到一个错误信息:HRESULT:0x800A03EC
解决:我有以下代码(现正整理!)
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 .xl上一页
的代码不具备根据对象模型的正确层次。
如果没有先定义一个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)
@Escounda - 查看我的编辑。首先,您需要声明并设置应用程序对象。然后是工作簿对象。然后工作表对象。然后范围对象 –
谢谢斯科特霍尔茨曼。我通过更正.find()的属性来解决它:XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious – Escounda
请将解决方案作为答案发布,而不是作为问题的更新。这是为了避免未来访客的混淆。这些更改不会完全丢失,您可以在[修订](https://stackoverflow.com/posts/46711104/revisions)中找到它们。 – Bugs