• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Word Macro to parse strings

Hello,

I am trying to get text from a Word table cell and do multiple parsing based on a newline. I'd like to do the following with the data below in a table cell:

String1
String1

string3 (very long, various lines...about a paragraph)

get string1 and store it in some variable,
get string2 and store it in some variable,
and finally get the rest of the text below string2 and store it in a variable.


Thanks
0
dr_country
Asked:
dr_country
  • 3
1 Solution
 
GrahamSkanRetiredCommented:
Sub CellText()
    Dim strText As String
    Dim p As Integer
    Dim iLen As Integer
    Dim strParts() As String
    Dim String1 As String
    Dim String2 As String
    Dim String3 As String
   
    strText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
    iLen = Len(strText)
    If iLen > 2 Then
        'drop trailing cell formatting characters
        strText = Left$(strText, iLen - 2)
        strParts = Split(strText, Chr$(11))
        If UBound(strParts) > 2 Then
            String1 = strParts(0)
            String2 = strParts(1)
            strParts(0) = ""
            strParts(1) = ""
            'rejoin and omit first two linefeed characters
            String3 = Mid$(Join(strParts, Chr(11)), 2)
        End If
    End If
End Sub

0
 
GrahamSkanRetiredCommented:
Hmm. Note that the 'Dim p As Integer' line is redundant.
0
 
dr_countryAuthor Commented:
Strange...I am not getting any values for any of the strings, just blanks. I actually tried using
oRow.Cells(2).Range.Paragraphs(1).Range.Text   and
oRow.Cells(2).Range.Paragraphs(2).Range.Text for the first two strings. But since the last string is quite long and consists of many 'newline' characters I cannot grasp it. Any suggestions?
0
 
GrahamSkanRetiredCommented:
First of all, if the first line can extracted via the paragraphs collection, then I have misinterpreted what you mean by 'newline'
In that case we could try chr$(13) for paragraph mark.

Sub CellText2()
    Dim strText As String
    Dim p As Integer
    Dim iLen As Integer
    Dim strParts() As String
    Dim String1 As String
    Dim String2 As String
    Dim String3 As String
   
    strText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
    iLen = Len(strText)
    If iLen > 2 Then
        'drop trailing cell formatting characters
        strText = Left$(strText, iLen - 2)
        strParts = Split(strText, Chr$(13))
        If UBound(strParts) > 2 Then
            String1 = strParts(0)
            String2 = strParts(1)
            strParts(0) = ""
            strParts(1) = ""
            'rejoin and omit first two linefeed characters
            String3 = Mid$(Join(strParts, Chr(13)), 2)
        End If
    End If
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now