2017-10-12 16 views
0

vsto outlook addinからExcelシートの最後の空のセルを検索するにはどうすればよいですか?vsto - VB - outlook addinの列の最後のセルを見つける

私はスコット・ホルツマンのコメントによると、私はそれを更新した次のコード(コンパイルではない)

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 - 私の編集を参照してください。まず、アプリケーションオブジェクトを宣言して設定する必要があります。次に、ワークブックオブジェクト。次に、ワークシートオブジェクト。その後、Range Object –

+0

Scott Holtzmanに感謝します。私は.find()のプロパティを次のように修正して解決しました:XlSearchOrder.xlByColumns、Excel.XlSearchDirection.xlPrevious – Escounda

関連する問題