|
-
Jan 13th, 2007, 01:31 PM
#1
Thread Starter
New Member
[RESOLVED] MS Word: How to parse RTF String
Hi,
I have a question.
I want to put a string through VBA in Word, which has a "RTF Syntax" in a
Word document, but parsed, that is formatted.
I'll give you an example:
VB Code:
Sub ParseRTF()
Dim txtStr as String
txtStr = "{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}\viewkind4\uc1\pard\lang1031\b\f0\fs17 This is an example of a String in RTF Format\par }"
End Sub
So, now I have a String with RTF. The problem is now, how can I put this
text in a Word Document, but it should be pasted already formated, not just
as a plain text.
One way of doing this, is putting the String in a .rtf File, and then using
this command:
VB Code:
Selection.InsertFile FileName:="C:\TEST.rtf", Link:=True
But I don't want to use a file, because of the time doing a disk read. I
want to paste the formatted string in the document directly.
I want to do this, because I have an MS Access Database, where I store the
RTF Strings in Memo Fields. I want to read them from the database, and
formatted, put them in a document.
Every help will be much appreciated.
Best Regards,
Robert Mileski
-
Jan 13th, 2007, 04:32 PM
#2
Re: MS Word: How to parse RTF String
this works, add a reference to richtx32.ocx (you may have to browse for it)
VB Code:
Dim rtb As RichTextLib.RichTextBox
Set rtb = New RichTextBox
mystr = "{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}\viewkind4\uc1\pard\lang1031\b\f0\fs17 This is an example of a String in RTF Format\par }"
rtb.TextRTF = mystr
Selection.TypeText rtb.Text
edit: you can declare your richtext box in the general section then set it once and use throughout your code
Last edited by westconn1; Jan 13th, 2007 at 04:35 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 13th, 2007, 06:12 PM
#3
Thread Starter
New Member
Re: MS Word: How to parse RTF String
Well, actually in the mean time I found a solution to my problem. Anyway thanks.
Using the RichTextBox Control doesn't solve the problem, because of the lack of RTF Commands. For Example, if you have a Field Code in the RTF, RichTextBox cannot process this, and you wont be able to paste it in word correctly, or for that matter it won't work also with any other complex Word formatting.
I will tell how I did it, so if someone else encounters the problem, here is the solution.
The trick is using the Windows Clipboard Api. You have to Declare it, and you also have to declare another variable "CF_RTF" which you do through the api call RegisterClipboardFormat("Richt Text Format") as long.
Now you send your string with the RTF to this API Call, which will now put your string in clipboard memory the right way, with the complex formatting that word has.
Now, you just place the Clipboard Data into your Document, with Selection.PasteSpecial wdData:=wdPasteRTF.
And you are done, problem solved. 
Best Regards,
Robert Mileski
-
Oct 1st, 2009, 06:10 PM
#4
New Member
Re: [RESOLVED] MS Word: How to parse RTF String
You have exactly the same situation as I do, but I am a total novice at this. Can you share the complete code you used to get the field from access to word.
-
Dec 7th, 2011, 02:42 PM
#5
New Member
Re: [RESOLVED] MS Word: How to parse RTF String
I am also interested in the code. Any chance we can see it?
-
Jul 10th, 2012, 03:54 PM
#6
New Member
Re: [RESOLVED] MS Word: How to parse RTF String
 Originally Posted by mileski
Hi,
I have a question.
I want to put a string through VBA in Word, which has a "RTF Syntax" in a
Word document, but parsed, that is formatted.
I'll give you an example:
VB Code:
Sub ParseRTF()
Dim txtStr as String
txtStr = "{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}\viewkind4\uc1\pard\lang1031\b\f0\fs17 This is an example of a String in RTF Format\par }"
End Sub
So, now I have a String with RTF. The problem is now, how can I put this
text in a Word Document, but it should be pasted already formated, not just
as a plain text.
One way of doing this, is putting the String in a .rtf File, and then using
this command:
VB Code:
Selection.InsertFile FileName:="C:\TEST.rtf", Link:=True
But I don't want to use a file, because of the time doing a disk read. I
want to paste the formatted string in the document directly.
I want to do this, because I have an MS Access Database, where I store the
RTF Strings in Memo Fields. I want to read them from the database, and
formatted, put them in a document.
Every help will be much appreciated.
Best Regards,
Robert Mileski
I am using MS RTF amplest to parse any texts into this sample. I managed to parse plain text (by replacing some TAGs placed insample. Now I am looking for the way to parse formatted text into RTF. Could you help me?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|