read line from table cell for vba in ms word  
Author Message
tsoetant





PostPosted: Visual Basic for Applications (VBA), read line from table cell for vba in ms word Top

Hi,

I need to read a line in ms word table cell. There are 3 line in a cell and I'd like to read line by line and put a line in a string variable. Would anyone be willing to help me here I'm so desperate with the answer since I tried everything and none are working.

What I did so far:
If (Mid(theTable.Cell(caseStartRow, 2).Range.Text, 1, _
Len(theTable.Cell(caseStartRow, 2).Range.Text) - 2)) = chrs$(13) Then

objFile.WriteLine ("")
objFile.WriteLine ("")
End If

Please help. Thanks so much!!


Microsoft ISV Community Center Forums2  
 
 
Navajo





PostPosted: Visual Basic for Applications (VBA), read line from table cell for vba in ms word Top

Have a look at the Split function. This seems to do what you want:

lineArray = Split(theTable.Cell(caseStartRow, 2).Range.Text, Chr$(13))


 
 
duck thing





PostPosted: Visual Basic for Applications (VBA), read line from table cell for vba in ms word Top

Here's an example of code that will do this:

Option Explicit

Public Sub main()
Dim strCellText As String
Dim strCellTextLines As New Collection
Dim vv As Variant

strCellText = ThisDocument.Tables(1).Cell(1, 1).Range.Text
Set strCellTextLines = ParseLines(strCellText)
MsgBox "Lines of text found = " & CStr(strCellTextLines.Count)

For Each vv In strCellTextLines
MsgBox vv
Next vv

Set strCellTextLines = Nothing
End Sub

Private Function ParseLines(tStr As String) As Collection
Dim tColl As New Collection, tptr As Integer, tlastptr As Integer, tCurrStr As String
tlastptr = 1

With tColl
Do
tptr = InStr(tlastptr, tStr, Chr(13))
If tptr = 0 Then Exit Do

tCurrStr = Mid(tStr, tlastptr, tptr - tlastptr)
tColl.Add tCurrStr

tlastptr = tptr + 1
Loop
End With

Set ParseLines = tColl
End Function

Feel free to email if you've got questions.



 
 
tsoetant





PostPosted: Visual Basic for Applications (VBA), read line from table cell for vba in ms word Top

Hi Navajo,

You are genius, you know! It works perfectly. Thank you so much!

Would you mind helping me for this problem:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=675347&SiteID=1

I know you know how to solve it, it's just a simple problem.

Thank you before!