Converting formatted MSWord text into an RTF string

Posted on 2004-09-09
Medium Priority
Last Modified: 2010-05-02

I'm running a VBA macro which uses the MSWord clipboard to store text with tracked changes. Then the macro would paste the text somewhere else WITH the tracked changes included. I noticed that if I do this manually, the tracked changes remain in the pasted text, but if I do it through the macro the changes dissapear when I paste. The rest of the formatting is preserved but I can't tell what was changed and what wasn't.

The macro does several things in between the copy and paste, so maybe one of those is causing it. The problem is, I can't change any of the middle procedures because the macro isn't mine (my company wants me to debug it) and they really haven't explained in detail what any of those procedures are supposed to do >:-( .

I'm figuring that if I could store the text with changes separately as an RTF string in a String variable, I could paste it later in a separate command. For example:

Sub CopyAsRTFandPaste()

'add a paragraph of text to the clipboard (the macro parses text by paragraphs)

Dim x as Range
Set x=Selection.Paragraphs(1).Range

'<<SOMEHOW CONVERT CLIPBOARD DATA TO RTF STRING -- that's where I'm stuck...>>

'the resulting string would look something like this:
'      "{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl" & _
       "{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}" & _
       "{\f2\froman\fprq2 Times New Roman;}}" & _
       "{\colortbl\red0\green0\blue0;\red255\green0\blue0;}" & _
       "\deflang1033\horzdoc{\*\fchars }{\*\lchars }" & _
       "\pard\plain\f2\fs24 Line 1 of \plain\f2\fs24\cf1" & _
       "inserted\plain\f2\fs24  file.\par }"

'then assign the string to a variable

Dim sRTF As String
sRTF = ConvertedRTFString

'then paste the formatted string with the API clipboard functions.

End Sub

       I found a good explanation on MSDN on how to paste a string like that to a document as formatted text, but I have no idea on the RTF conversion part. Any help?

BTW, if there's any other way to do what I need to do without the RTF method, please let me know too :). The problem is that the tracked changes get erased when the "x.Paste" method is invoked.


Question by:ljquirin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 76

Expert Comment

ID: 12015625
What's your word version? I'm usiing Word 2000.

What the current past ecode look like.

This will paste OK, but doesn't copy the deleted, only the new, text

Dim rng As Range
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
rng.PasteSpecial , , wdInLine, False, wdPasteRTF

Author Comment

ID: 12015835
Hi again,

I'm using Office 2003 (the VBA is pretty much similar to Word XP).

The current copy ans paste codes look like this:

Set rng=Selection.Paragraphs(1).Range

(then a bunch of procedures)


I've been using regular paste so far, but I'll try the PasteSpecial instead (will let you know what happens). The toughest part for me, though, is finding out how to convert text from the clipboard into RTF code before I paste it.

      One more note: There's a bug in Word 2003 that if you copy a text with changes when TrackChanges is on and then turn TrackChanges off, this accepts all the changes made in the clipboard text and the changes don't show up in red when you paste. There are some "If... then" statements between the Copy and Paste commands which access the ActiveDocument.TrackRevisions and ActiveDocument.ShowRevisions properties, but don't change them. Does anyone know if that could be the problem?

LVL 76

Accepted Solution

GrahamSkan earned 750 total points
ID: 12015887
If PasteSpecial works, you won't have to learn RTF. Fingers crossed.

Author Comment

ID: 12026474

It looks like I couldn't solve it with PasteSpecial :-( I contacted the people who asked me to debug the macro and apparently the program is tied to an EXE file which accesses the clipboard directly and somehow ends up discarding the tracked changes as soon as I copy the text (they might have said that EARLIER...). I did find out that the changes can be stored properly as RTF or formatted text, though, so no problem there. I just need one of two things:
1) another storage method for the RTF strings that doesn't use the MS Word clipboard (what I was trying to do with the String variable previously), or
2) some way to store more than one string on the clipboard.

Any ideas?


PS: When you say "won't have to learn RTF" I suppose the conversion can't be done easily with Windows or API functions. Is there some macro or DLL out there that can do that?

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question