2017-10-12 70 views
0

如何從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

+0

請將解決方案作爲答案發布,而不是作爲問題的更新。這是爲了避免未來訪客的混淆。這些更改不會完全丟失,您可以在[修訂](https://stackoverflow.com/posts/46711104/revisions)中找到它們。 – Bugs

回答

0

解決:我有以下代碼(現正整理!)

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上一頁

0

的代碼不具備根據對象模型的正確層次。

如果沒有先定義一個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) 
+0

@Escounda - 查看我的編輯。首先,您需要聲明並設置應用程序對象。然後是工作簿對象。然後工作表對象。然後範圍對象 –

+0

謝謝斯科特霍爾茨曼。我通過更正.find()的屬性來解決它:XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious – Escounda

相關問題