×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

string in rtf

string in rtf

string in rtf

(OP)
Hi,
I've a variable that contains a string in rtf format..
Is it possible using vbs to decode this string in txt "standard"?

THANKS

RE: string in rtf

It depends how you are decoding. Are you looking for a built-in function to do it or are you reading in a string and parsing the RTF? Does it contain bold/italic text? Does the text change size?

Assuming it is straight text, and you are parsing the string,

1) Look for {
2) Inside the { will be keywords followed by \. These are the tokens that are space terminated.
3) Skip all the { and tokens until you come to something that does not begin with \. This is the text you require.
4) Extract until you get to }
5) Skip the rest

RE: string in rtf

(OP)
Hi,
tahnks for your answer..
The string is like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\fs20 test to clean words
\par }

I don't know how can I parse and clean it..
I would like to extract only this text "test to clean words"..The tags may change if I have a bold string format etc

THANKS

RE: string in rtf

> variable that contains a string in rtf format.

From where are you getting that string?

RE: string in rtf

(OP)
Hi,
it's a sql field

RE: string in rtf

Sure, ok, but a SQL field in what? How are you getting this data? I ask because it may be simpler to parse this in the source application. VBScript has very limited capability in this area (it cannot use the VB6 RIched ActiveX control, it cannot make RichEd API calls, it cannot instantiate a TOM object from the RichEd DLL). If you were using PowerShell you could do it in about 2 lines ...

RE: string in rtf

So here's a solution where VBScriot drives a Powershell script

First, the Powershell script (note that by default Powershell scripts are disabled for security reasons. Follow the instructions on this page to enable RemoteSigned execution policy), which you should save as "cleanrichtext.ps1"

CODE

# cleanrichtext.ps1
# Author: Mike Strong
# Datew: 5 Dec 2019
# Simple example of converting RTF to plain text for tek-tips
param (
    # default rtf if argument not passed
    [string]$in = "{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\fs20 example only\par }" 
)
Add-Type -AssemblyName System.Windows.forms # make sure assembly is available
$rtfBox = New-Object System.Windows.Forms.RichTextBox
try # see if we can parse the rtf successfully
{
    $rtfBox.rtf=($in)
}
catch
{
    $rtfBox.text="Source is badly formed rich text" # oops, not good RTF
}
Write-Output $rtfBox.Text 

Now the example VBScript that leverages the powershell script:

CODE

' Author: Mike Strong
' 05 Dec 2019
' Example for converting RTF into plain text using powershell from VBScript
pscommand = "d:\downloads\ps_scripts\cleanrichtext.ps1" ' the powershell script
rtf="'{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\fs20 test to clean words\par }'"
cmd = "powershell "  & pscommand & " -in " & rtf ' Warning: maximum commandline length 8190 characters 
Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec(cmd)
exec.StdIn.Close
strPlainText=exec.StdOut.ReadAll
MsgBox strPlainText 

Note that this will briefly flash the powershell console window

RE: string in rtf

(OP)
Hi,

thanks for your help..
It works with you vbs but if I run it on my variable (that contains my rtf..I've always the warning "Source is badly formed rich text" as it cannot recognize the rtf format
All my fields are like that:
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\fs20 AAAAA \par }
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\fs16 testssssssssssss dddd dddddd PDF/EXCEL ddddddd.\fs20 \par }
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\fs20 bbbbbbbbbbbbbb. \b BX 1500 \par \b0 bjjjjjjjjjjjjjjjj. \par }

RE: string in rtf

Would need to see your script, as I strongly suspect that you are passing your variable incorrectly

RE: string in rtf

(OP)
Yes sure is like that:

If ObjAdoDbRecordSet_10.EOF=False Then
DESCRESTESA=ObjAdoDbRecordSet_10("DESCRESTESA")
'-------------------
pscommand = "N:\cleanrichtext.ps1" ' the powershell script
rtf=ObjAdoDbRecordSet_10("DESCRESTESA")
cmd = "powershell " & pscommand & " -in " &rtf ' Warning: maximum commandline length 8190 characters
Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec(cmd)
exec.StdIn.Close
DESCRESTESA=exec.StdOut.ReadAll
MsgBox "DESCRESTESA "&DESCRESTESA
'--------------
End If

THANKS!!!!!!!!!!!!!!!!!!!!!

RE: string in rtf

Yep, incorrectly passed. Look at how I build my example rtf more closely:

CODE

rtf="'{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\fs20 test to clean words\par }'" 

So try

CODE

rtf = "'" & ObjAdoDbRecordSet_10("DESCRESTESA") & "'" 

RE: string in rtf

(OP)
yesssssssssssssssss

thanks!!!!!!!!!!!!!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close