Consulting

Results 1 to 4 of 4

Thread: Solved: can't remove carriage return

  1. #1
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location

    Solved: can't remove carriage return

    I often copy and paste text into an email. Sometimes each line has a carriage return (CR) which terminates the line. How does the script look like that would delete all CRs in a selected paragraph of text?

    In Word I use the following script. It does not work in Outlook.

    [VBA]Sub DeleteCR()

    With Selection
    .Text = Replace(.Text, vbCr, " ")
    End With

    End Sub[/VBA]

    In PowerPoint I use another script. That also doesn't work in Outlook.

    [VBA]Sub DeleteCRs()
    Dim oshp As Shape
    Dim otxt As TextRange
    'check for type of selection
    'replaces all text in shape unless
    'text is highlighted
    If ActiveWindow.Selection.Type = ppSelectionNone _
    Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
    If ActiveWindow.Selection.Type = ppSelectionShapes Then
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    Set otxt = oshp.TextFrame.TextRange
    End If
    If ActiveWindow.Selection.Type = ppSelectionText Then
    Set otxt = ActiveWindow.Selection.TextRange
    If Len(otxt) < 1 Then
    Set otxt = otxt.Parent.Parent.TextFrame.TextRange
    End If
    End If
    With otxt
    .Text = Replace(.Text, vbCr, " ")
    End With
    With otxt
    .Text = Replace(.Text, ". ", ". " & vbCr)
    End With
    End Sub[/VBA]

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,285
    Location
    [VBA]Sub use_selection_of_text_in_body()
    'The mail is open and a selection is made before
    'running this macro !!!
    'themessage is the word editor we are going to use
    Dim themessage As Object
    'is the selection of the mailbody
    Dim mydoc As Object
    'the string of the selection
    Dim mytext As String
    'create the word editor
    Set themessage = Application.ActiveInspector.WordEditor
    'print to debug window (ctrl+g to display it)
    Debug.Print themessage
    'get the selection of text
    Set mydoc = themessage.windows(1).Selection
    'print to debug window
    Debug.Print mydoc
    'store mydoc to string variable
    'if format needs to be retained, maybe look for
    'search and replace stuff in word
    mytext = mydoc.Text
    'use replace to get rid of enter
    mytext = Replace(mytext, vbCr, "")
    'print to debug window
    Debug.Print mytext
    'get new text into message.
    'note : original format (lettertype, formatting, ...) is lost
    themessage.windows(1).Selection = mytext
    End Sub[/VBA]Charlize

  3. #3
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    great! That is working. Thanks Charlize.

    Now, I just need to figure out how to turn this macro into an add-in.
    That is, if that is possible at all in Outlook.

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Just fire up a copy of VB6 and you can turn the code into an addin.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •