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
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
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
'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
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
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
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
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
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.
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:
if odoc.tables.count > 0 then
set otable = tables(t) ' where t is between 1 and tables.count
else
set otable = tables.add(odoc.selection.range, listview1.listitems.count, listview1.coulumnheaders.count)
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
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
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
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:
Private Sub Form_Click()
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 = odoc.Tables.Add(odoc.Paragraphs(1).Range, lv1.ListItems.Count + 2, lv1.ColumnHeaders.Count)
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
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
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
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
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