VB6 Code in Exporting Listview Items to Excel

Microsoft Excel Logo

Microsoft Excel Logo (Photo credit: AJC1)

Assuming that we have used a listview in displaying our data in Visual Basic 6, and we named our listview as LvList having seven (7) fields. Now, to export all the list of data from our listview to an Excel file, we will use the code below.

If, for example, you have less than or more than seven (7) fields in your listview, just decrease or increase the number of cells to fit your needs. Also, don’t forget to change LvList to the name of the listview you are using. Place the below code to your export button. Once clicked, it will export all the data from your listview to an Excel file automatically.

Dim ExcelObj As Object
Dim ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer

Set ExcelObj = CreateObject(“Excel.Application”)
Set ExcelBook = ExcelObj.WorkBooks.Add
Set ExcelSheet = ExcelBook.WorkSheets(1)

With ExcelSheet
For i = 1 To LvList.ListItems.Count
.cells(i, 1) = LvList.ListItems(i).Text
.cells(i, 2) = LvList.ListItems(i).SubItems(1)
.cells(i, 3) = LvList.ListItems(i).SubItems(2)
.cells(i, 4) = LvList.ListItems(i).SubItems(3)
.cells(i, 5) = LvList.ListItems(i).SubItems(4)
.cells(i, 6) = LvList.ListItems(i).SubItems(5)
.cells(i, 7) = LvList.ListItems(i).SubItems(6)
End With

ExcelObj.Visible = True

Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelObj = Nothing

The above code in Visual Basic 6 (VB6) in exporting listview items to an Excel file really helped me a lot. Hope it will help you too. If you’ll encounter some problems, just comment below and I’ll be happy to provide you help.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s