Solved

read and write rtf file as binary

Posted on 2010-09-05
26
641 Views
Last Modified: 2012-05-10
How can i read and write the contents of a richtextbox ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
  • 3
  • +2
26 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33609151
0
 

Author Comment

by:isnoend2001
ID: 33609182
Yes that is what i have been using, however I don't want the file to be able to be opened with word
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33609206
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:isnoend2001
ID: 33609226
thanks Dhaest:, but that is written in C#, i am using vb6
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33609244
Reading and Writing Binary Files In Visual Basic
http://www.vb6.us/tutorials/reading-and-writing-binary-files-visual-basic
0
 
LVL 3

Expert Comment

by:Genetic_Wolf
ID: 33611525
The RTF (Rich Text Format) Will ALWAYS be readable by word, no matter what.
RTF format was the FIRST format Microsoft used in Word 1.0

howerver, if you want to prevent your user to Double click on a file and open it,
just use a different extention.

save it for exemple as: myFile.rtz

if user are double clicking the file, Microsoft will ask with what the user want to open it.
but if the user, chose Word as opening device, or if user open Word, and choose file/Open and select your file, no matter how you recorded it, if it's RTF, Word will open it.

if you need a more solid method to prevent your user to open your RTF, you encapsulate your file into a different mime type and then binary save it.  You'll need to reverse the process in order to see the file in your program though.

if you explain to us the goal of this, maybe we can select a better solution for you.
if you're only trying to hide a msg and you'll NOT need to edit that message after, you would be better to create an image of the message.  That's more easy to hide.
0
 

Author Comment

by:isnoend2001
ID: 33611826
Thanks Genetic_Wolf:
I have already changed the extension, but it can still be viewed in word and wordpad. Think i will try to put it into a udt and add some fake fields, but i still need a way to save and read as binary
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33611909
If you do not want people to be able to load the file in  word, you need to encrypt the file. You can get the full content of the Rich Text Box with RichTextBoxName.TextRTF, where RichTextBoxName is the name of your file. For Example,
StringName$ = RichTextBox1.TextRTF
stores the entire contents of RichTextBox1, including formating, in StringName$. Then encrypt this string. It can be something as simple as
For X = 1 to Len(StringName$)
     Mid$(StringName$,X,1) = Chr$(Asc(Mid$(StringName$,X,1)) +1)
Next X
You can then save the string with something like
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1

(You might want to Kill the file first if it already exists.)
You can then recover the file with
 
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
'Decrypt the string here.

RichTextBox1.TextRTF = S$
 
0
 

Author Comment

by:isnoend2001
ID: 33611966
Thanks Leithauser:
What data type would StringName$ and S$ be declared as ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33612062
The '$' is old-school shorthand declaration for the String data type.

Here is the actual MS documentation:
http://support.microsoft.com/default.aspx?scid=kb;en-us;191713

          %                 Integer
          &                 Long
          !                 Single
          #                 Double
          $                 String
          @                 Currency
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33612252
<<What data type would StringName$ and S$ be declared as>>
String. As mentioned my Idle Mind, adding a $ to the end of a varialbe name is an old (but still valid) way of specifying that a variable is a string. It predates the Dim, Declare, Public, Private, and Global keywords.
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33612269
BTW, the variable should be either S$ or StringName$ or whatever. I got a little confused in my typing and accidentally used S$ in some places and StringName$ in others. It should always be hte same variable name. That is,
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
should be
Open "FileName" For Binary As 1
StringName$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
0
 

Author Comment

by:isnoend2001
ID: 33612344
Leithauser:
The file cannot be read by word, however it does not load into the rtf either, maybe i am doing something wrong:
Sub OpenRTF()
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
 
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
    Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) + 1)
Next X

Rtf1.TextRTF = S$
End Sub
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33612357
DId you encrypt the file before saving it? If so, did you decrypt it where I said
'Decrypt the string here.
0
 

Author Comment

by:isnoend2001
ID: 33612374
Yes i Encrypted before saving and does this not decrypt the string:
'Decrypt the string here.
For X = 1 To Len(StringName$)
    Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) + 1)
Next X
as the code above
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33612388
The decryption would be
For X = 1 To Len(StringName$)
    Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X
 
0
 

Author Comment

by:isnoend2001
ID: 33612452
Leithauser: I changed the decryption, but still a blank rtf
Sub OpenRTF()
'Dim sText As String
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
 
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
   Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X

Rtf1.TextRTF = S$
End Sub
0
 
LVL 5

Accepted Solution

by:
Leithauser earned 500 total points
ID: 33612487
You need to change
S$ = Space$(LOF(1))
and
Rtf1.TextRTF = S$

to
StringName$ = Space$(LOF(1))
and
Rtf1.TextRTF = StringName$
In fact, do  search of the code I gave you for any reference to S$ and change it to StringName$.
When I was typing the code for you, I accidentally switched back and forth between S$%, which I normally use, and StringName$, which I thought was more descriptive.
 

0
 

Author Closing Comment

by:isnoend2001
ID: 33612530
Thanks that works
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33612593
Thanks for the points. I need to make 3000 points per month to maintain my own free unlimited question points, plus the fact that I am now just 3,306 more points until Master rating. One more question this month will maintain my free question points, and two more should get me my Masters rating. :-)
BTW, I think if you use
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
to write a shorter string when you had prevuiously a longer one in that file, you MAY leave trailing characters from your original longer string on the new one in the file (not sure about this). This is why I suggested that you kill the file before writing to it again. You can do this with
If Len(Dir$("FileName")) Then
    Kill "FileName"
End If
 before

Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
0
 

Author Comment

by:isnoend2001
ID: 33612730
Thanks for adding that. If your last fix did not work I was going to go to Renta Coder and pay to have it written as i have about 50 times before.. You saved me $.
I am writing in xp and this  file is for my son who uses Win7. win7 does not like using App.path so some rewriting, setup and manifest is next.
0
 
LVL 3

Expert Comment

by:Genetic_Wolf
ID: 33614169
still readable in Word though...
the encrypt function is not very solid all you have to do is substract 1 letter from what you read.
and if you got a caracter 255, the function will crash.

if you really need a security function, tell me.
0
 

Author Comment

by:isnoend2001
ID: 33614373
Thanks Genetic_Wolf, but for this purpose it works as everything is in images. Looks like this:
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fdee9886de857ecd645ggggggg11gggggggggggged9185ee9d
86ed9d86e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd111111e9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fd111111e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd111111111111111111e9f:fde9f:fde9f:fd1111
11111111111111e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fdee9886de857ecd64
5ggggggg11gggggggggggged9185ee9d86ed9d86e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9

If i need something stronger i found Platform SDK Redistributable: CAPICOM
0
 
LVL 5

Expert Comment

by:Leithauser
ID: 33614447
Note that the encryption technique I gave is very simple, and was really just intended as an example of a simple encryption system, not a definitive solution. I figured you would want to write your own, so that only you would know it. Otherwise, anyone looking at Experts-Exchange would know your encryption.

In addition, it appears from your question that you were not really looking for a high security solution, just something that would stop the casual user from loading the file in Word. Since RTF relies on a rather rigid set of coding to interpret the file formatting, changing or rearranging even a few characters makes it unreadable in Word.
0
 

Author Comment

by:isnoend2001
ID: 33614575
Leithauser:
your solution was just what i asked for, Thanks
0
 
LVL 3

Expert Comment

by:Genetic_Wolf
ID: 33647741

I was just pointing out that it was VERY far from the original question asked.
and if user give more explanation at what he intend to do or what goal he want to achieve, Expert like us can sometime give a more appropriate answer.

but I'm glad the solution satisfy him, good work.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

615 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