Paste unformatted text with red text

M

Macro''''er

Hello,

I'm looking to create a macro to paste whatever it is in the clipboard with
unformatted text with red font color. How can this be accomplished?

Thanks so much!
 
J

Jay Freedman

Hello,

I'm looking to create a macro to paste whatever it is in the clipboard with
unformatted text with red font color. How can this be accomplished?

Thanks so much!

First, in the VBA editor, go to Tools > References and put a checkmark
in the box next to "Microsoft Forms 2.0 Object Library". Then insert
this macro:

Sub demo()
Dim myRg As Range
Dim myDO As DataObject
Dim myText As String

On Error GoTo bye

Set myRg = Selection.Range
Set myDO = New DataObject
myDO.GetFromClipboard
myText = myDO.GetText(1)

With myRg
.Text = myText
.Font.Color = wdColorRed
End With

bye:
Set myRg = Nothing
End Sub



This was my second choice. First I tried simply

MyRg.PasteSpecial DataType:=wdPasteText

but, unlike setting MyRg.Text, this didn't cause the range to expand
to include the pasted text. It pasted OK, but then I didn't have the
correct range to apply the font color.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Doug Robbins - Word MVP

With Selection.Range
.Paste
.Font.Color = wdColorRed
.Text = .Text
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top