• Search for soft return (VBA / wORD / 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Search for soft return (VBA / wORD / 2000)

    Author
    Topic
    #361093

    I need to be able to find when information has wrapped in a Word Table Cell – I initially though that I could search for a soft return using LF and attempted using the following code:

    Sub Test()
    Dim rngCellTest As Range

    Set rngCellTest = ActiveDocument.Tables(1).Cell(2, 3).Range

    rngCellTest.Find.ClearFormatting
    With rngCellTest.Find
    .Text = vbLf
    .Replacement.Text = “”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    If rngCellTest.Find.Execute = True Then
    MsgBox “true”
    Else
    MsgBox “false”
    End If
    End Sub

    This doesn’t work. Does anyone have any suggestions?

    Viewing 1 reply thread
    Author
    Replies
    • #545124

      Cathy,

      The special characters for doing a find on a manual line break aka soft return are “^l” (caret-pipe – dunno what else you call it) so perhaps you could try substituting this for vbLf as the Find text.

      When you say “information has wrapped in a Word table cell”, you’re referring specifically to lines that have a soft return at the end? – ’cause text will wrap in a Word table cell with or without a soft return (and it would be pretty hard to track down the latter via code I’d say).

      Gary

      • #545128

        Gary – have you actually used that combination? Only it’s not caret-pipe, it’s caret-small L … I use it frequently when straightening out much-forwarded emails … smile

        • #545138

          Beryl,

          Oops – what was I thinking? blush
          Thanks for straightening me out – caret-small el it is!

          Gary

      • #545274

        Thanks for all the suggestions, but I am trying to determine when text wraps in a table cell. From your responses, ‘soft return’ was the incorrect term (but I now know how to find one!!).

        Any more suggestions?

        • #545282

          This is a crude method: push the insertion point down a line and see if you are still in the same cell (by checking the row number or whether you might have exited the table entirely). This will tell you whether there is more than one line of text in a cell, but can’t tell you whether this happened because the line wrapped automatically or the user entered a break of some kind. My testing was limited, YMMV.

          Function LineWrapped(myCell As Range) As Boolean
          myCell.Collapse wdCollapseStart
          myCell.Select
          'those two lines probably could be reversed: select the cell, then collapse the selection
          Selection.MoveDown wdLine, 1, wdMove
          If Selection.Information(wdWithInTable) = False Or _
              (Selection.Information(wdEndOfRangeRowNumber)  _
                  myCell.Information(wdEndOfRangeRowNumber)) Then
              'the line must not continue or you would still be in
              'the original range
              LineWrapped = False
          Else
              LineWrapped = True
          End If
          Application.GoBack
          Set myRange = Nothing
          Application.ScreenRefresh
          End Function

          Hope this helps.

          • #545285

            I am a bit limited because I can’t use the selection object (I will be running the application from VB controlling Word, which will be invisible). All code relates to application.document.range.

            The closest I have come to solving this problem is using the selection object to work out VerticalPositionRelativeToTextBoundary – this will return >0 if there is more than one line within the cell. Code is as follows:

            Sub Test4()
            Dim rngTest As Range
            Set rngTest = ActiveDocument.Tables(1).Cell(2, 3).Range
            rngTest.Select
            Selection.EndKey Unit:=wdLine
            MsgBox Selection.Information(wdVerticalPositionRelativeToTextBoundary)
            End Sub

            EndKey only applies to the selection object, but if I can find an alternative using a range, this is what I would like to use.

            Any further suggestions?

            Cathy

            • #545301

              A little bit simpler, if you are going to select the cell range, would be to then call the Word Count dialog – this will return a lines count for the selection.

              Forgive me for not posting code – am severely sleep-deprived! doze

            • #545401

              Cathy,

              Here’s your code revised to illustrate using the WordCount dialog to return a line count for the cell:

              Public Sub GetRowCountInTableCell()
              Dim rngTest As Range
              Dim lngLines As Long
              
              Set rngTest = ActiveDocument.Tables(1).Cell(2, 3).Range
              rngTest.Select
              'must select the text; not the cell itself:
              Selection.MoveEnd Unit:=wdCharacter, Count:=-1
              
              With Dialogs(wdDialogToolsWordCount)
                  lngLines = .Lines
                  '.Display rather than .Execute seems to be needed
                  'due to apparently sticky .Lines property
                  .Display 1
              End With
              
              MsgBox "This cell contains " & lngLines & " line(s) of text."
              
              Selection.Collapse
              Set rngTest = Nothing
              End Sub
              

              Gary

    • #545148

      The codes are different in a table cell, you need to look for the sequence 13 and 7.

      • #545256

        No, ^13^7 is the end-of-cell marker (there are two of those at the end of a row).
        The manual line break ^l has the ASCII code ^11. The VBA constant vbVerticalTab happens to have the same code, so you could use that, too, but it sure would confuse any Word user.
        To locate the end of lines that break because the text does not fit in a single line, you would need the Selection, and .Expand(wdLine) or .MoveDown(wdLine).

        cheersRegards, Klaus

        • #545264

          Right, I was testing for 13 7.
          vbVerticalTab is 11 and is one of the dumbest names for a constant.

    Viewing 1 reply thread
    Reply To: Search for soft return (VBA / wORD / 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: