Consulting

Results 1 to 7 of 7

Thread: Replacement text includes a chr(13)

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    Replacement text includes a chr(13)

    I need to markup topics with tags. It works ok for an inline word but a single word followed by a return gives this tag

    before

    Creation

    after selection of word and macro run

    <link>Creation
    </link>Creation

    I want it to be in one line and no space after the word creation, in between the tags
    <link>Creation</link>Creation



    Private Sub CommandButton7LinkTopicsMarkup_Click()
    
    
    InSelection = False
    If selection.Type = wdSelectionIP Then InSelection = True
    If InSelection = True Then
        MsgBox ("select some text")
        Exit Sub
    End If
    
    
    'remove any spaces after word
    While selection.Characters.Last = " "
        If selection.Characters.Last = " " Then
            selection.MoveEnd Unit:=wdCharacter, Count:=-1
        End If
    Wend
    
    Dim textOriginal As selection
    Set textOriginal = selection
    
    
        With selection.Range
            .InsertBefore "<link>" & textOriginal & "</link>"
        End With
    
    
    End Sub
    I was just trying to figure a way and came up with this to modify the code in red. It works. However is there a proper way to deal with this?

    While selection.Characters.Last = " " Or selection.Characters.Last = Chr(13)
        If selection.Characters.Last = " " Then
            selection.MoveEnd Unit:=wdCharacter, Count:=-1
        End If
        If selection.Characters.Last = Chr(13) Then
            selection.MoveEnd Unit:=wdCharacter, Count:=-1
        End If
        
    Wend

  2. #2
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Try this:
    Private Sub CommandButton7LinkTopicsMarkup_Click()
    InSelection = False
    If Selection.Type = wdSelectionIP Then InSelection = True
    If InSelection = True Then
        MsgBox ("select some text")
        Exit Sub
    End If
    
    'remove a return following selection
    While Selection.Characters.Last = vbCr
        If Selection.Characters.Last = vbCr Then
            Selection.MoveEnd Unit:=wdCharacter, Count:=-1
        End If
    Wend
    'remove spaces following selection
    While Selection.Characters.Last = " "
        If Selection.Characters.Last = " " Then
            Selection.MoveEnd Unit:=wdCharacter, Count:=-1
        End If
    Wend
    Dim textOriginal As Selection
    Set textOriginal = Selection
    
    With Selection.range
            .InsertAfter "</link>" & textOriginal  '& "</link>"
            .InsertBefore "<link>" '& textOriginal & "</link>"
    End With
    End Sub

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    ok thank you for an alternative way. I will add this to my knowledge.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Simpler:
    With selection.Range
      .InsertBefore "<link>" & Split(textOriginal,vbCr)(0) & "</link>"
    End With
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Quote Originally Posted by macropod View Post
    Simpler:
    With selection.Range
      .InsertBefore "<link>" & Split(textOriginal,vbCr)(0) & "</link>"
    End With
    ok but this does not remove any spaces that might be before the chr(13). I need the tagged text to not have an spaces at the end.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    So use:
    With selection.Range
      .InsertBefore "<link>" & Trim(Split(textOriginal,vbCr)(0)) & "</link>"
    End With
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Many thanks for this Paul. I had not known about Split and I had tried Trim at the wrong point.
    Out of interest. Can one control the font and colour...(let me try)... ok I figured this addition to control font and colour.


    If CheckBox6Bold.Value = True Then
          textOriginal.Font.Bold = True
    End If
    If CheckBox7Blue.Value = True Then
        textOriginal.Font.ColorIndex = wdBlue
    End If

Posting Permissions

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