[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • 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
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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