Converting formatted MSWord text into an RTF string

Posted on 2004-09-09
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
  • 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 250 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

837 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