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