Results 1 to 18 of 18

Thread: [RESOLVED] Export to Word from listview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] Export to Word from listview

    I have listview contains record and column name.. How I can export it to Microsoft Word.. I able to export to Excel, but don't know how to export this to Word

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    you can automate word very similarly to excel, how you put the information into a word document depends on how you want it set out, you can put into paragraphs table or list, possibly some other ways too,
    try recording macro in word then adapt the code to your vb6 objects
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    Hi here is code to export to excel from listview. I am no idea to modify this code to work in Word.

    Code:
     'set to excell
        Dim objExcel As New Excel.Application
        Dim bkWorkBook As Workbook
        Dim shWorkSheet As Worksheet
        Dim i As Integer
        Dim j As Integer
    
        Set objExcel = New Excel.Application
        Set bkWorkBook = objExcel.Workbooks.add
        Set shWorkSheet = bkWorkBook.ActiveSheet
        For i = 1 To ListView1.ColumnHeaders.count
            shWorkSheet.Cells(1, Chr(64 + i)) = ListView1.ColumnHeaders(i)
        Next
        For i = 1 To ListView1.ListItems.count
            shWorkSheet.Cells(i + 2, "A") = ListView1.ListItems(i).Text
            For j = 2 To ListView1.ColumnHeaders.count
                shWorkSheet.Cells(i + 2, Chr(64 + j)) = ListView1.ListItems(i).SubItems(j - 1)
            Next
        Next
        
        objExcel.Visible = True
        layervisiblelity 'set visible lity
        Set objExcel = Nothing
        Set bkWorkBook = Nothing
        Set shWorkSheet = Nothing
    I want to display it as tables in Word

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    Code:
     'set to excell
        Dim objword As word.Application
        Dim odoc As document
        dim otable as table
        Dim i As Integer
        Dim j As Integer
    
        Set objword = New word.Application
        Set odoc = objword.documents.add
        Set otable = odoc.tables.add(selection.range,5,5)
        ' change the above for the position of the table and the rows and columns you require, which are probably the coulumns count and rows count of the listview
        For i = 1 To ListView1.ColumnHeaders.count
    '***** working with word tables is a bit different from excel, try like this
            otable.rows(1).cells( i) = ListView1.ColumnHeaders(i)
        Next
        For i = 1 To ListView1.ListItems.count
            otable.columns(1).cells( i) = ListView1.ListItems(i).Text
            For j = 2 To ListView1.ColumnHeaders.count
               otable.rows(i+2).cells( j)= ListView1.ListItems(i).SubItems(j - 1)
            Next
        Next
        
        objword.Visible = True
    
        Set objword = Nothing
        Set odoc= Nothing
        Set otable= Nothing
    i have not tested this at all with a listview
    Last edited by westconn1; Jul 12th, 2008 at 12:52 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    I got undefine . I have add the reference Microsoft Office 11.0 Object Library

    word.Application

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    you need reference to word 11 or whatever version
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    Ok..

    I change and add word 11.0 Object Library..

    I got error at this line.


    Method not Found. What else reference I miss it out?

    Code:
    otable.rows(1).cells( i) = ListView1.ColumnHeaders(i)

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    sorry copy error
    otable.rows(1).cells( i).range.text = ListView1.ColumnHeaders(i)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    I try to export whole contents of the listview. I got object variable not set at this line. I don't have selection.Range

    Code:
    Set otable = odoc.Tables.add(selection.Range, 5, 5)


    Code:
    Dim objword As Word.Application
        Dim odoc As Document
        Dim otable As Word.Range
       ' Dim otable As Table
        Dim i As Integer
        Dim j As Integer
        Set objword = New Word.Application
        Set odoc = objword.Documents.add
        ' Get the current document's range object
        Set otable = odoc.Tables.add(selection.Range, 5, 5)
        ' change the above for the position of the table and the rows and columns you require, which are probably the coulumns count and rows count of the listview
        For i = 1 To ListView1.ColumnHeaders.count
    '***** working with word tables is a bit different from excel, try like this
           ' otable.Rows(1).Cells(i) = ListView1.ColumnHeaders(i)
        otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)
        Next
        For i = 1 To ListView1.ListItems.count
            otable.Columns(1).Cells(i).Range.Text = ListView1.ListItems(i).Text
            For j = 2 To ListView1.ColumnHeaders.count
               otable.Rows(i + 2).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)
            Next
        Next
        
      ' Show Word to the user
        objword.Visible = True
        Set objword = Nothing
        Set odoc = Nothing
        Set otable = Nothing

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    I unable to copy or export the tables into Microsoft Word.. I got error, no tables at this location.. I don't know why.. How I can copy this content in the listview to microsoft word

    Code:
    Dim objword As Word.Application
        Dim odoc As Document
        Dim otable As Word.Range
        Dim i As Integer
        Dim j As Integer
        Set objword = CreateObject("Word.Application")
        Set odoc = objword.Documents.add
        ' Get the current document's range object
        Set otable = odoc.Range
        For i = 1 To ListView1.ColumnHeaders.count
        otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)
        Next
        
        For i = 1 To ListView1.ListItems.count
            otable.Columns(1).Cells(i).Range.Text = ListView1.ListItems(i).Text
            For j = 2 To ListView1.ColumnHeaders.count
               otable.Rows(i + 2).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)
            Next
        Next
        
        Dim sTemp As String
        ' Insert a heading on the string
        sTemp = "Customer ID" & vbTab & "Company Name" & _
        vbTab & "Contact Name" & vbCrLf & sTemp
      ' Insert the data into the Word document
        otable.Text = sTemp
        ' Convert the text to a table and format the table
        otable.ConvertToTable vbTab, , , , wdTableFormatColorful2
      ' Show Word to the user
        objword.Visible = True
        Set objword = Nothing
        Set odoc = Nothing
        Set otable = Nothing
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    I got the link from Visual Basic FAQ with title How do I add/modify/read a table programmatically?

    http://www.vbforums.com/showthread.php?t=402098


    but it not help me a lot. i don't find a tutorial how to write the tables from listview to Microsoft Word, I hope somebody could update it and let me know.
    Last edited by matrik02; Jul 12th, 2008 at 08:44 AM.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    you have otable declared as a word range, should be a table

    if you do not know what to declare as use object until you can get the correct type

    if the table already exists in the document you do not need to add a table, but if no table you will get an error trying to work with a table
    vb Code:
    1. if odoc.tables.count > 0 then
    2. set otable = tables(t) ' where t is between 1 and tables.count
    3. else
    4. set otable = tables.add(odoc.selection.range, listview1.listitems.count, listview1.coulumnheaders.count)
    5. end if
    if you create a new document each time then you will always need to add a table
    Last edited by westconn1; Jul 12th, 2008 at 05:26 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    When I declare it as table, I got method or data member not found at this line . This is because .Rows not found. That why I declare otable as word.range

    Code:
    otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)



    Full code is below
    Dim objword As Word.Application
    Dim odoc As Document
    Dim otable As Table
    Dim i As Integer
    Dim j As Integer
    Set objword = CreateObject("Word.Application")
    Set odoc = objword.Documents.add
    ' Get the current document's range object
    Set otable = Tables.add(odoc.selection.Range, ListView1.ListItems.count, ListView1.ColumnHeaders.count)
    For i = 1 To ListView1.ColumnHeaders.count
    otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)
    Next

    For i = 1 To ListView1.ListItems.count
    otable.Columns(1).Cells(i).Range.Text = ListView1.ListItems(i).Text
    For j = 2 To ListView1.ColumnHeaders.count
    otable.Rows(i + 2).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)
    Next
    Next

    Dim sTemp As String
    ' Insert a heading on the string
    sTemp = "Customer ID" & vbTab & "Company Name" & _
    vbTab & "Contact Name" & vbCrLf & sTemp
    ' Insert the data into the Word document
    otable.Text = sTemp
    ' Convert the text to a table and format the table
    otable.ConvertToTable vbTab, , , , wdTableFormatColorful2
    ' Show Word to the user
    objword.Visible = True
    Set objword = Nothing
    Set odoc = Nothing
    Set otable = Nothing



  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    i thiink the problem is with the selection object which i mormally try to avoid, try like this
    Set otable = odoc.Tables.Add(odoc.Paragraphs(1).Range,ListView1.ListItems.Count, ListView1.ColumnHeaders.Count)

    edit i tested this in an existing program with a list view, works with no errors
    vb Code:
    1. Private Sub Form_Click()
    2. Dim objword As Word.Application
    3. Dim odoc As Document
    4. Dim otable As Table
    5. Dim i As Integer
    6. Dim j As Integer
    7. Set objword = CreateObject("Word.Application")
    8. Set odoc = objword.Documents.Add
    9. ' Get the current document's range object
    10. Set otable = odoc.Tables.Add(odoc.Paragraphs(1).Range, lv1.ListItems.Count + 2, lv1.ColumnHeaders.Count)
    11. For i = 1 To lv1.ColumnHeaders.Count
    12. otable.Rows(1).Cells(i).Range.Text = lv1.ColumnHeaders(i)
    13. Next
    14.  
    15. For i = 1 To lv1.ListItems.Count
    16. otable.Columns(1).Cells(i).Range.Text = lv1.ListItems(i).Text
    17. For j = 2 To lv1.ColumnHeaders.Count
    18. otable.Rows(i + 2).Cells(j).Range.Text = lv1.ListItems(i).SubItems(j - 1)
    19. Next
    20. Next
    21.  
    22. Dim sTemp As String
    23. ' Insert a heading on the string
    24. sTemp = "Customer ID" & vbTab & "Company Name" & _
    25. vbTab & "Contact Name" & vbCrLf & sTemp
    26. ' Insert the data into the Word document
    27. 'otable.Text = sTemp
    28. ' Convert the text to a table and format the table
    29. 'otable.ConvertToTable vbTab, , , , wdTableFormatColorful2
    30. ' Show Word to the user
    31. objword.Visible = True
    32. Set objword = Nothing
    33. Set odoc = Nothing
    34. Set otable = Nothing
    35.  
    36. End Sub
    Last edited by westconn1; Jul 12th, 2008 at 06:18 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    Ok. I got the column header into Microsoft Word.. but I cannot get the listitems for the listview..I got the error message like this the requested member of collection does not exist. And the error found at this line

    Code:
      otable.Rows(i + 2).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)

    Full code is here

    Code:
    Dim objword As Word.Application
        Dim odoc As Word.Document
        Dim otable As Word.Table
        Dim i As Integer
        Dim j As Integer
        Set objword = New Word.Application
        Set odoc = objword.Documents.add
        odoc.Activate
        objword.selection.Move Unit:=wdStory
        objword.selection.TypeParagraph
        objword.selection.TypeParagraph
        Set otable = odoc.Tables.add(odoc.Paragraphs(1).Range, ListView1.ListItems.count, ListView1.ColumnHeaders.count, wdWord9TableBehavior, wdAutoFitWindow)
        For i = 1 To ListView1.ColumnHeaders.count
        otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)
        Next
        For i = 1 To ListView1.ListItems.count
            otable.Columns(1).Cells(i).Range.Text = ListView1.ListItems(i).Text
            For j = 2 To ListView1.ColumnHeaders.count
               otable.Rows(i + 2).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)
            Next
        Next
        objword.Visible = True
        Set objword = Nothing
        Set odoc = Nothing
        Set otable = Nothing

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Export to Word from listview

    you need more rows, see my code above
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Export to Word from listview

    It work...

  18. #18
    New Member
    Join Date
    Nov 2018
    Posts
    1

    Re: [RESOLVED] Export to Word from listview

    After year a little adjustment:

    Dim objword As Word.Application
    Dim odoc As Word.Document
    Dim otable As Word.Table
    Dim i As Integer
    Dim j As Integer
    Set objword = New Word.Application
    Set odoc = objword.Documents.Add
    odoc.Activate
    objword.Selection.Move Unit:=wdStory
    objword.Selection.TypeParagraph
    objword.Selection.TypeParagraph
    Set otable = odoc.Tables.Add(odoc.Paragraphs(1).Range, ListView1.ListItems.Count + 1, ListView1.ColumnHeaders.Count, wdWord9TableBehavior, wdAutoFitWindow)
    For i = 1 To ListView1.ColumnHeaders.Count
    otable.Rows(1).Cells(i).Range.Text = ListView1.ColumnHeaders(i)
    Next
    For i = 1 To ListView1.ListItems.Count
    otable.Columns(1).Cells(i + 1).Range.Text = ListView1.ListItems(i).Text
    For j = 2 To ListView1.ColumnHeaders.Count
    otable.Rows(i + 1).Cells(j).Range.Text = ListView1.ListItems(i).SubItems(j - 1)
    Next
    Next
    objword.Visible = True
    Set objword = Nothing
    Set odoc = Nothing
    Set otable = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width