Home > Enterprise >  Macro to duplicate text then hide the duplicated text on MS Word keeping the formatting
Macro to duplicate text then hide the duplicated text on MS Word keeping the formatting

Time:09-16

I want to duplicate the text from a word document (everything including headers, footers, tables and textboxes), then I would like to hide the original text (or the hidden one) while keeping the formatting with a macro.

I've do some research and tried to make something, here what I've done until now :

Dim text As Word.Range
Set text = Selection.Range.Duplicate
Selection.InsertParagraphAfter
Selection.InsertAfter Text:=text.Text
text.Font.Hidden = True

The problem with this macro is that doesn't copy the format of the text, its make a copy of the text as a "plain text".

You have an idea of how to keep the formatting?

CodePudding user response:

The code below should do what you need. You need to ensure that the source range does not include the start of the target range.

Dim source As Range, target As Range
With ActiveDocument
    .Content.InsertParagraphAfter
    Set source = .Content
    source.MoveEnd wdParagraph, -1
    .Characters.Last.FormattedText = source.FormattedText
    source.Font.Hidden = True
End With
  • Related