Stripping vbCrLf from strings 
Author Message
 Stripping vbCrLf from strings

I have a macro that pulls strings out of a Word document.  How can I strip
the carriage return/line feed(s)?

Also, is it possible to use Debug.Print or something else to automatically
delete the contents of the Immediate window before outputting new text?



Mon, 21 Jul 2003 07:12:01 GMT  
 Stripping vbCrLf from strings
Try something like this. It probably does both more and less than
you need.

Andrew

Private Function Trim_lines(Stext)
'*** Trim lines in the string to remove leading or trailing blank
*lines*
'   A single blank line is output as such
'   Note that to preserve manual indentation, *leading* blank chars
aren't trimmed
Dim I1, I2, SS, S
SS = Stext
'   *** Trim leading blank lines
Do
    I1 = InStr(SS, vbCr)                'find first CR
    If I1 = 0 Then Exit Do              'no CRs
    S = Left(SS, I1 - 1)
    If Trim(S) <> "" Then Exit Do       'not only whitespace before
    If Mid(SS, I1 + 1, 1) = vbLf Then I1 = I1 + 1  'add one for LF
    SS = Mid(SS, I1 + 1)                'remove whitespace and CR/LF
Loop
'   *** Trim trailing blank lines
Do
    I1 = 0
    Do
        I2 = InStr(I1 + 1, SS, vbCr)    'find last CR
        If I2 = 0 Then Exit Do
        I1 = I2
    Loop
    If I1 = 0 Then Exit Do              'no CRs
    If Mid(SS, I1 + 1, 1) = vbLf Then
        S = Mid(SS, I1 + 2)             'after last CRLF
    Else
        S = Mid(SS, I1 + 1)             'after last CR
    End If
    If Trim(S) <> "" Then Exit Do       'not only whitespace before
    SS = Left(SS, I1 - 1)               'remove CR/LF and whitespace
Loop
Trim_lines = SS
End Function 'Trim_lines

Quote:

> I have a macro that pulls strings out of a Word document.  How can I strip
> the carriage return/line feed(s)?

> Also, is it possible to use Debug.Print or something else to automatically
> delete the contents of the Immediate window before outputting new text?

--
Andrew Gabb

phone: +61 8 8342-1021, fax: +61 8 8269-3280
-----


Mon, 21 Jul 2003 09:45:25 GMT  
 Stripping vbCrLf from strings
It turned out that the dohickeys on the end of my strings were
vbVerticalTabs.  I used this to trim 'em off:

Function strTrimVTabs(str As String) As String

    While Left(str, 1) = vbVerticalTab
        str = Right(str, Len(str) - 1)
    Wend

    While Right(str, 1) = vbVerticalTab
        str = Left(str, Len(str) - 1)
    Wend

    strTrimVTabs = str

End Function

Thanks for your help tho.


Quote:
> Try something like this. It probably does both more and less than
> you need.

> Andrew

> Private Function Trim_lines(Stext)
> '*** Trim lines in the string to remove leading or trailing blank
> *lines*
> '   A single blank line is output as such
> '   Note that to preserve manual indentation, *leading* blank chars
> aren't trimmed
> Dim I1, I2, SS, S
> SS = Stext
> '   *** Trim leading blank lines
> Do
>     I1 = InStr(SS, vbCr)                'find first CR
>     If I1 = 0 Then Exit Do              'no CRs
>     S = Left(SS, I1 - 1)
>     If Trim(S) <> "" Then Exit Do       'not only whitespace before
>     If Mid(SS, I1 + 1, 1) = vbLf Then I1 = I1 + 1  'add one for LF
>     SS = Mid(SS, I1 + 1)                'remove whitespace and CR/LF
> Loop
> '   *** Trim trailing blank lines
> Do
>     I1 = 0
>     Do
>         I2 = InStr(I1 + 1, SS, vbCr)    'find last CR
>         If I2 = 0 Then Exit Do
>         I1 = I2
>     Loop
>     If I1 = 0 Then Exit Do              'no CRs
>     If Mid(SS, I1 + 1, 1) = vbLf Then
>         S = Mid(SS, I1 + 2)             'after last CRLF
>     Else
>         S = Mid(SS, I1 + 1)             'after last CR
>     End If
>     If Trim(S) <> "" Then Exit Do       'not only whitespace before
>     SS = Left(SS, I1 - 1)               'remove CR/LF and whitespace
> Loop
> Trim_lines = SS
> End Function 'Trim_lines


> > I have a macro that pulls strings out of a Word document.  How can I
strip
> > the carriage return/line feed(s)?

> > Also, is it possible to use Debug.Print or something else to
automatically
> > delete the contents of the Immediate window before outputting new text?

> --
> Andrew Gabb

> phone: +61 8 8342-1021, fax: +61 8 8269-3280
> -----



Tue, 22 Jul 2003 01:46:51 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. vbcrlf or vbCrLf

2. Strip Spaces from string

3. Strip spaces from string

4. String.Trim does not strip

5. Need Regular Expression to strip non-numeric characters from string

6. how to strip a string

7. how to strip a string

8. stripping a string - Newbie

9. Pls Help Newbie: Stripping Strings of CR and LF

10. Help Newbie - String stripping...?

11. Strip Tab from String

12. HELP! Stripping NON ALPHA characters from a string

 

 
Powered by phpBB® Forum Software