0

I've been working on a VBA code to paste in a text string that will always have the same formatting. The text string will always include starting and ending quotation marks followed by text. I want to put the text string with the quotation marks in a parenthetical at the end of the remaining text string. It is very important that I keep the original source formatting for emphasis (italics, bold, etc.), while taking on the style characteristics of the paragraph where it is pasted. Meaning, I want to essentially use a modified version of the merge formatting paste. I also want the cursor to go to the end of the pasted text.

Original Text: "This "is" the "text," which I want to copy." John Doe v. Doe, 470 U.S. 99 (1999)

Pasted Text after Code: John Doe v. Doe, 470 U.S. 99 (1999) ("This "is" the "text," which I want to copy.")

I've taken a stab at the coding, but it's not working as I would like.

Sub ExampleCode()
Application.ScreenUpdating = False
Dim RngA As range, RngB As range, StrTmp As String
Set RngA = Selection.range
With RngA
.Paste
Do While .Characters.Last Like "[ " & Chr(11) & vbCr & "]"
.End = .End - 1
Loop
StrTmp = .Text
.Text = ""
.InsertBefore StrTmp
.MoveStartUntil Chr(34), wdBackward
.MoveEndUntil Chr(34), wdBackward
.MoveEndUntil Chr(34), wdBackward
.MoveEndUntil Chr(34), wdBackward
.MoveEndUntil Chr(34), wdBackward
Set RngB = .Duplicate
.Collapse wdCollapseStart
.Text = " (" & RngB.Text & ")"
.FormattedText = RngB.FormattedText
RngB.Text = vbNullString
End With
Application.ScreenUpdating = True
End Sub
broncos15
  • 59
  • 1
  • 8
  • 1
    Strings don't hold formatting. What you need is as simple as: Set `RngA` to the text you want to move. Set `RngB` to the location you want the text moved to. Then use the `FormattedText` property to copy the original and delete `RngA` – Timothy Rylatt Jan 28 '23 at 08:46
  • How can I do this? I have been able to sort of get the formatting part correct, but it's not moving my text. The new code is: [/code] Strings don't hold formatting. What you need is as simple as: Set RngA to the text you want to move. Set RngB to the location you want the text moved to. Then use the FormattedText property to copy the original and delete RngA [/code] – broncos15 Apr 10 '23 at 18:42

0 Answers0