Pete
Pete

Reputation:

Convert Excel VBA to VB.Net with Selection.ListObject.QueryTable Intact

I need to access the Selection.ListObject.QueryTable object in order to preserve the column width.

The code is as follows:

Range("B9").Select()
With Selection.ListObject.QueryTable
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
End With

What is the VB.Net version of this Excel-generated VBA code?

Upvotes: 0

Views: 2834

Answers (1)

Phillip Wells
Phillip Wells

Reputation: 7662

What about something like this?

Dim excelApp AS Object = CreateObject("Excel.Application")
excelApp.Workbooks.Open(Filename:=_file)

With excelApp.ActiveWorkbook.Worksheets(0).Cells(9, 2).QueryTable
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = 1
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
End With

where _file is the name of your Excel file.

Upvotes: 5

Related Questions