Copy and Paste RTF string

M

melon

I have a RTF string strTest set to the following:

{\rtf1\ansi\ansicpg1252\deff0\deftab360{\fonttbl{\f0\fswiss Arial;}
{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Riched20 5.50.99.2050;}\viewkind4\uc1\pard
\f0\fs20\lang1033\par
\par
\f1 TEST\f0\par
}

I am trying to copy it using the following

Dim MyData As DataObject
Set MyData = New DataObject

MsgBox strTest ' make sure I am copying the correct
thing
MyData.SetText strTest , -16639 ' VBCFRTF doesn't
work
MyData.PutInClipboard

However, nothing is put into the clipboard, when I try to run

objWord.ActiveDOcument.Bookmarks("Body").Range.Paste

I will get a Command Fail error.
 
K

Klaus Linke

Hi,

I'd save the RTF string as a temp.RTF file in the Windows Temp folder,
insert that RTF file in the document, then kill the file.
Unless it results in a real speed problem, that'd probably be the simplest
solution.

Klaus
 
M

melon

Hi,

I'd save the RTF string as a temp.RTF file in the Windows Temp folder,
insert that RTF file in the document, then kill the file.
Unless it results in a real speed problem, that'd probably be the simplest
solution.

Klaus

How do you do that? For some reason my search term "VBA save rtf
file" is not very helpful.
 
K

Klaus Linke

How do you do that? For some reason my search term
"VBA save rtf file" is not very helpful.


Quick and dirty/not debugged:

Sub RTF_Place(sText As String)
Dim iFile As Integer
iFile = FreeFile
Open "C:\Windows\Temp\temp.RTF" For Binary As #iFile
Put #iFile, , sText
Close #iFile
Selection.InsertFile FileName:="C:\Windows\Temp\temp.rtf", Range:="", _
ConfirmConversions:=False
Kill "C:\Windows\Temp\temp.RTF"
End Sub

Sub Test_RTF_Place()
Dim sText As String
sText = "{\rtf1\ansi\ansicpg1252\deff0\deftab360{\fonttbl{\f0\fswiss
Arial;}{\f1\fswiss\fcharset0 Arial;}}{\*\generator Riched20
5.50.99.2050;}\viewkind4\uc1\pard\f0\fs20\lang1033\par\par\f1 TEST\f0\par}"

Call RTF_Place(sText)

End Sub

If you plan to use the macro on several computers, it might be a good idea
to google for a safe way to determine the Temp folder... or maybe just use
the current folder -- you'll delete the file anyway.

Regards,
Klaus
 

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