Results 1 to 6 of 6

Thread: [RESOLVED] MS Word: How to parse RTF String

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Resolved [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:
    1. Sub ParseRTF()
    2.     Dim txtStr as String
    3.  
    4.         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 }"
    5.  
    6. 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:
    1. 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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Dim rtb As RichTextLib.RichTextBox
    2. Set rtb = New RichTextBox
    3. 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 }"
    4. rtb.TextRTF = mystr
    5. 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    6

    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

  4. #4
    New Member
    Join Date
    Oct 2009
    Posts
    1

    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.

  5. #5
    New Member
    Join Date
    Dec 2011
    Posts
    1

    Re: [RESOLVED] MS Word: How to parse RTF String

    I am also interested in the code. Any chance we can see it?

  6. #6
    New Member
    Join Date
    Jul 2012
    Posts
    1

    Re: [RESOLVED] MS Word: How to parse RTF String

    Quote Originally Posted by mileski View Post
    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:
    1. Sub ParseRTF()
    2.     Dim txtStr as String
    3.  
    4.         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 }"
    5.  
    6. 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:
    1. 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
  •  



Click Here to Expand Forum to Full Width