Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Converting formatted MSWord text into an RTF string

Hello,

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
x.Copy

...
...
'<<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.

Thanks,
LQ

0
ljquirin
Asked:
ljquirin
  • 2
  • 2
1 Solution
 
GrahamSkanCommented:
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
0
 
ljquirinAuthor Commented:
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
rng.Copy

...
(then a bunch of procedures)
...

rng.paste


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?

Thanks,
LQ
0
 
GrahamSkanCommented:
If PasteSpecial works, you won't have to learn RTF. Fingers crossed.
0
 
ljquirinAuthor Commented:
Hello,

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?

LQ

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?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now