PDA

View Full Version : [SOLVED:] Replacement text includes a chr(13)



JPG
03-11-2020, 09:47 AM
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

Kilroy
03-11-2020, 12:48 PM
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

JPG
03-11-2020, 01:21 PM
ok thank you for an alternative way. I will add this to my knowledge.

macropod
03-11-2020, 02:16 PM
Simpler:

With selection.Range
.InsertBefore "<link>" & Split(textOriginal,vbCr)(0) & "</link>"
End With

JPG
03-11-2020, 02:44 PM
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.

macropod
03-11-2020, 08:09 PM
So use:

With selection.Range
.InsertBefore "<link>" & Trim(Split(textOriginal,vbCr)(0)) & "</link>"
End With

JPG
03-12-2020, 03:06 AM
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