vba ms word - read line from table cells

J

john smith

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!
 
D

Doug Robbins - Word MVP

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

--
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
 
H

Helmut Weber

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

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

john smith

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
 
J

john smith

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

Doug Robbins - Word MVP said:
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

--
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

john smith said:
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!
 
J

john smith

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
 
H

Helmut Weber

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]

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

john smith

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
 
H

Helmut Weber

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!

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top