0
votes

I have this macro I use by manually selecting the text I want to duplicate (in MS Word).

testoin = Selection.Text

testout = testoin & Chr(13) & testoin

Selection.Text = testout

I have been trying to get VBA to hide (Font hide) the "original" portion of the text (i.e. the testoin before the Chr(13) using MoveEnd MoveLeft etc. to no avail. Could you please help?

2

2 Answers

0
votes

When modifying text in a Word document it is best to work in Word rather extract text to VBA and then put it back.

Dim myRange As Word.Range
    ' Make a copy of the range  in a new range object
    ' If we don't use duplicate then myRange will be the same range object as Select
    ' so that any changes in the Selection range will be reflected in myRange.
    Set myRange = Selection.Range.Duplicate
    ' Insert Chr(13)
    Selection.InsertParagraphAfter
    ' Duplicate the text using the duplicate range
    Selection.InsertAfter text:=myRange.text
    ' Apply hidden text  to the original text.
    myRange.Font.Hidden = True

If your hidden text does not disappear from view it will be because you have Word set to display formatting (Show/Hide icon in the paragraph tab on the Ribbon)

0
votes

I found a way to make it do what I need, re-using your code:

Sub duplicator3()
'
' duplicator3 Macro
'
    Dim Range1 As Range, Range2 As Range
    Set myRange1 = Selection.Range.Duplicate  
    Set myRange2 = Selection.Range.Duplicate
    Selection.InsertParagraphAfter
    ' Duplicate the text using the duplicate range
    Selection.InsertAfter Text:=myRange1.Text
    ' Apply hidden text  to the original text.

    myRange1.Font.Hidden = True

    Selection.InsertAfter Text:=myRange2.Text

End Sub

Thanks!