Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string in rtf 1

Status
Not open for further replies.

elly00

Technical User
Jun 30, 2011
69
IT
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
 
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
 
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
 
> variable that contains a string in rtf format.

From where are you getting that string?
 
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 ...
 
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:
[blue][COLOR=green]# cleanrichtext.ps1
# Author: Mike Strong
# Datew: 5 Dec 2019
# Simple example of converting RTF to plain text for tek-tips[/color]
param (
    [COLOR=green]# default rtf if argument not passed[/color]
    [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 [COLOR=green]# make sure assembly is available[/color]
$rtfBox = New-Object System.Windows.Forms.RichTextBox
try [COLOR=green]# see if we can parse the rtf successfully[/color]
{
    $rtfBox.rtf=($in)
}
catch
{
    $rtfBox.text="Source is badly formed rich text" [COLOR=green]# oops, not good RTF[/color]
}
Write-Output $rtfBox.Text[/blue]

Now the example VBScript that leverages the powershell script:

Code:
[blue][COLOR=green]' Author: Mike Strong
' 05 Dec 2019
' Example for converting RTF into plain text using powershell from VBScript[/color]
pscommand = "d:\downloads\ps_scripts\cleanrichtext.ps1" [COLOR=green]' the powershell script[/color]
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 [COLOR=green]' Warning: maximum commandline length 8190 characters[/color] 
Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec(cmd)
exec.StdIn.Close
strPlainText=exec.StdOut.ReadAll
MsgBox strPlainText[/blue]

Note that this will briefly flash the powershell console window
 
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 }
 
Would need to see your script, as I strongly suspect that you are passing your variable incorrectly
 
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!!!!!!!!!!!!!!!!!!!!!
 
Yep, incorrectly passed. Look at how I build my example rtf more closely:

Code:
[blue]rtf="[COLOR=red][b]'[/b][/color]{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\fs20 test to clean words\par }[COLOR=red][b]'[/b][/color]"[/blue]

So try

Code:
[blue]rtf = "'" & ObjAdoDbRecordSet_10("DESCRESTESA") & "'"[/blue]
 
yesssssssssssssssss

thanks!!!!!!!!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top