MS Office Forum / Word / Programming / August 2006
vba ms word - read line from table cells
|
|
Thread rating:  |
john smith - 22 Aug 2006 20:27 GMT Hi guys,
I 'd like to read line by line in table cells. I have 3 lines in table cell and would like to read line by line in these table cells and put this line into a variable. Would any of you know how to do this?
Please help :(
Any help would be greatly appreciated
Many Thanks!
Doug Robbins - Word MVP - 22 Aug 2006 21:18 GMT What separates the lines in the cells? Is it carriage returns [Enter], or line breaks [Shift+Enter] or do the lines just wrap within the cell.
If it is carriage returns, it means that each line is a separate paragraph and you can simply get hold of the .Range of each paragraph in the cells. Otherwise, you are going to have to move the selection from line to line and then use the .Range of the "\line" bookmark
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Hi guys, > [quoted text clipped - 7 lines] > > Many Thanks! john smith - 22 Aug 2006 21:57 GMT Hi Doug,
A table cell contain something like this:
Folders Home¶
§ Logon to CMC as administrator¶
§ Click on the "Folders" icon on the CMC home page¶
I want to put the first line into variable A, 2nd line to var B and so on. Thus write it into text file with comma separated like A,B,C
What I got so far is:
//detect end of line
If (Mid(theTable.Cell(caseStartRow, 2).Range.Text, 1, _
Len(theTable.Cell(caseStartRow, 2).Range.Text) - 2)) = chrs$(13) Then
//put each line into a variable
A = ...
B = ...
C = ...
//write it to text file with comma separated
objfile.write (A)
objfile.write (",")
objfile.write (B)
objfile.write (",")
objfile.write (C)
End If
Would you know how to do this in vba word macro? Thanks you so much!
Regards,
John
> What separates the lines in the cells? Is it carriage returns [Enter], or > line breaks [Shift+Enter] or do the lines just wrap within the cell. [quoted text clipped - 15 lines] >> >> Many Thanks! Helmut Weber - 22 Aug 2006 21:24 GMT Hi John,
lines or paragraphs (¶) ?
If there are truely 3 lines, how are they seperated? Do they wrap automatically or is there a chr(11) (linefeed, vbverticaltab) at their end? Are there cells which contain nothing but linefeeds plus the end of cell mark? Unlikely, but possible.
Just to get you started, see this code to get the number of lines in (most) cells. Put the insertionpoint in any cell of a table:
Sub Test455() Dim rngTmp As Range ' a cell's range Dim lngCnA As Long ' just a counter Set rngTmp = selection.Cells(1).Range If Len(rngTmp) = 2 Then ' nothing in the cell but end of cell mark lngCnA = 1 Else rngTmp.End = rngTmp.End - 1 ' exclude end of cell mark from the range lngCnA = rngTmp.ComputeStatistics(wdStatisticLines) End If MsgBox lngCnA End Sub
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
john smith - 22 Aug 2006 21:57 GMT Hi Helmut,
A table cell contain something like this:
Folders Home¶
§ Logon to CMC as administrator¶
§ Click on the "Folders" icon on the CMC home page¶
I want to put the first line into variable A, 2nd line to var B and so on. Thus write it into text file with comma separated like A,B,C
What I got so far is:
//detect end of line
If (Mid(theTable.Cell(caseStartRow, 2).Range.Text, 1, _
Len(theTable.Cell(caseStartRow, 2).Range.Text) - 2)) = chrs$(13) Then
//put each line into a variable
A = ...
B = ...
C = ...
//write it to text file with comma separated
objfile.write (A)
objfile.write (",")
objfile.write (B)
objfile.write (",")
objfile.write (C)
End If
Would you know how to do this in vba word macro? Thanks you so much!
Regards,
John
> Hi John, > [quoted text clipped - 26 lines] > MsgBox lngCnA > End Sub Helmut Weber - 22 Aug 2006 22:19 GMT Ok,
so we are talking about _paragraphs_ in a cell.
How about the last _line_ in the cell? Is it empty? Or is there some text ending in an end-of-cell mark?
Like: Paragraph1¶ Paragraph2¶ SomeText[end-of-cell]
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
john smith - 22 Aug 2006 22:35 GMT Folders Home¶
§ Logon to CMC as administrator¶
§ Click on the "Folders" icon on the ¶ CMC home page¶
Cell Position: row 3 column 2
Hi Helmut,
Thanks for the quick reply! It's not a paragraph. Just lines with bullet in a table cell. I'd like to put each line into a separate variable for each and looping and storing the line value into string variable for each line all the way until the end of the table rows.
Example are like above, just like that. No other texts exists at the end of cell.
Code so far but didn't work: For i = caseStartRow + 2 To caseEndRow Dim rngTmp As Range ' a cell's range Dim lngCnA As Long ' just a counter Set rngTmp = (Mid(theTable.Cell(caseStartRow, 2).Range.Text, 1, _ Len(theTable.Cell(caseStartRow, 2).Range.Text) - 2)) MsgBox rngTmp If Len(rngTmp) = 2 Then ' nothing in the cell but end of cell mark lngCnA = 1 Else rngTmp.End = rngTmp.End - 1 ' exclude end of cell mark from the range lngCnA = rngTmp.ComputeStatistics(wdStatisticLines) End If MsgBox lngCnA Next
Would you know how to do that? Thank you so much, Helmut! Really2 appreciated!
Regards, John
Helmut Weber - 22 Aug 2006 23:13 GMT Hi John,
I'm sorry, but what you wanna do is, transforming the contects of a table into a two dimensional array of lines of cells. Can be done, but it's hell.
Not to speak of bullets.
If you wan't to write it to a consistent comma separated file, you'd have first to find out the cell with the most lines. Then add lines to all the cells with less lines than the cell with the most lines, until all cells got the same number of lines.
I did this once, for converting Word-tables to Quark-Xpress.
Some hundred lines of code.
Keep on, break it all down to smaller steps!
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
john smith - 22 Aug 2006 21:56 GMT Hi Guys,
A table cell contain something like this:
Folders Home¶
§ Logon to CMC as administrator¶
§ Click on the "Folders" icon on the CMC home page¶
I want to put the first line into variable A, 2nd line to var B and so on. Thus write it into text file with comma separated like A,B,C
What I got so far is:
//detect end of line
If (Mid(theTable.Cell(caseStartRow, 2).Range.Text, 1, _
Len(theTable.Cell(caseStartRow, 2).Range.Text) - 2)) = chrs$(13) Then
//put each line into a variable
A = ...
B = ...
C = ...
//write it to text file with comma separated
objfile.write (A)
objfile.write (",")
objfile.write (B)
objfile.write (",")
objfile.write (C)
End If
Would you know how to do this in vba word macro? Thanks you so much!
Regards,
John
Doug Robbins - Word MVP - 23 Aug 2006 04:36 GMT See "Convert Labels into Mail Merge Data File" on fellow MVP Graham Mayor's website at:
http://www.gmayor.com/convert_labels_into_mail_merge.htm
Maybe something like that can be used.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Hi Guys, > [quoted text clipped - 44 lines] > > John
|
|
|