Solved

Converting formatted MSWord text into an RTF string

Posted on 2004-09-09
5
222 Views
Last Modified: 2010-05-02
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
Comment
Question by:ljquirin
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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
0
 

Author Comment

by:ljquirin
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
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
 
LVL 76

Accepted Solution

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

Author Comment

by:ljquirin
ID: 12026474
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now