Home > Software engineering >  Superscript Formatting Erased when Text is stored in String
Superscript Formatting Erased when Text is stored in String

Time:05-26

Dim ST As String
ST = ActiveDocument.Paragraphs(1).Range.Text

In my document, Paragraphs(1) is actually 2 32. However, with Debug.Print ST, the output is 2 32. Is there any way to store the data without compromising the superscript and subscript formatting?

The objective behind this is to store 5 lines in ST(1 to 5) and then shuffle the order of the 5 lines.

CodePudding user response:

You could store them as range within a collection

Sub shuffle()

Dim col As Collection: Set col = New Collection

Dim p As Paragraph
For Each p In ThisDocument.Paragraphs
    col.Add p.Range
Next

dim rg as range
Set rg = ThisDocument.Content
rg.Collapse wdCollapseEnd

'print the paragraphs to the end of the document in reversed order
Dim i As Long
For i = col.Count To 1 Step -1
    rg.InsertAfter col(i).Text
Next

End Sub

CodePudding user response:

It's not clear how do you want to capture the paragraphs so I'm assuming that you will have those paragraphs selected, modify it based on your requirement:

FormattedText property can be used to replace a range with formatted text so this should work for you:

Private Sub ShuffleSelectedParagraphs()
        
    ActiveDocument.Content.InsertParagraphAfter
    
    Dim i As Long
    For i = Selection.Paragraphs.Count To 1 Step -1
        ActiveDocument.Content.Paragraphs.Last.Range.FormattedText = Selection.Paragraphs(i).Range.FormattedText
    Next
End Sub

You will need to select the paragraphs first then run the Sub, it will duplicate the selected paragraphs at the end of the document but in the reverse order.

  • Related