Home All Groups Group Topic Archive Search About

[function LOF] Fails after WordPad saving

Author
30 Jan 2006 2:04 AM
andyIT
I need to load the whole textual content
(with tags and so on...)
of a .rtf file into a String variable.

Like this:
myVar = "{\rtf1\ansi\ansicpg1252\deff0\deflang1040\def..."

----------------


When I try to read a .rtf file
previously saved by 'MS WordPad'
the LOF instruction does fail.

Note:
'MS WordPad' adds at the very bottom of the document a " " char
that I think it "cheats" the LOF instruction.
'MS Word' adds no " " char instead, and all goes regular.



Here my code:


Dim mytxt as String
Dim fnum As Integer

fnum = FreeFile


    Open "C:\myfolder\mytest.rtf" For Input As fnum

        mytxt = Input(LOF(CLng(fnum)), #fnum)

    Close fnum


-------------------


Also,
I tried with the 'While Not EOF' instruction,
but things go worse,
bacause it cuts the first 3 chars of each line.


Here my code:


Dim mytxt as String
Dim mytxtTEMP as String
Dim fnum As Integer


fnum = FreeFile

    Open "C:\myfolder\mytest.rtf" For Input As fnum

        While Not EOF(fnum)
            Line Input #fnum, mytxtTEMP
            mytxt = mytxt & mytxtTEMP
        Wend

    Close fnum



---------------------------------



How can I safely read a .rtf file ?
(I need to load the content into a String variable)



----------------------------------

Author
30 Jan 2006 3:16 AM
Dmitriy Antonov
Show quote Hide quote
"andyIT" <and***@inwind.it> wrote in message
news:43dd73eb.3019184@aioe.cjb.net...
>
> I need to load the whole textual content
> (with tags and so on...)
> of a .rtf file into a String variable.
>
> Like this:
> myVar = "{\rtf1\ansi\ansicpg1252\deff0\deflang1040\def..."
>
> ----------------
>
>
> When I try to read a .rtf file
> previously saved by 'MS WordPad'
> the LOF instruction does fail.
>


Take a look at this
http://support.microsoft.com/kb/142246/en-us

Dmitriy
Author
30 Jan 2006 2:52 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"andyIT" <and***@inwind.it> wrote in message
news:43dd73eb.3019184@aioe.cjb.net...

> Dim mytxt as String
> Dim fnum As Integer
>
> fnum = FreeFile
>
>
>    Open "C:\myfolder\mytest.rtf" For Input As fnum
>
>        mytxt = Input(LOF(CLng(fnum)), #fnum)
>
>    Close fnum

Why are you using CLng()? It's not breaking your code, but there's no point
in doing it.
Author
31 Jan 2006 1:08 AM
AndyIT
You are right, initially I thought it was failing
because fnum could have been a Long variable.
I was desperately trying all the solutions...
Author
30 Jan 2006 3:56 PM
Rick Rothstein [MVP - Visual Basic]
> I need to load the whole textual content
> (with tags and so on...)
> of a .rtf file into a String variable.
>
> Like this:
> myVar = "{\rtf1\ansi\ansicpg1252\deff0\deflang1040\def..."
>
> When I try to read a .rtf file
> previously saved by 'MS WordPad'
> the LOF instruction does fail.

This code will do what you want...

   Dim mytxt As String
   Dim fnum As Integer
   fnum = FreeFile
   Open "C:\myfolder\mytest.rtf" For Binary As fnum
     mytxt = Space(LOF(fnum))
     Get fnum, , mytxt
   Close fnum

I have a question for you though... Why are you doing this? Is the ultimate
aim to put the text loaded into the 'mytxt' variable into a RichTextBox? If
so, why not simply load the file directly into the RichTextBox like this...

   RichTextBox1.LoadFile "C:\myfolder\mytest.rtf"

If you still need the file stored in a variable, you can load it up from the
RichTextBox itself like this...

   mytxt = RichTextBox1.TextRTF

Rick
Author
31 Jan 2006 1:25 AM
AndyIT
Yes, I've read the previous post and I used the Binary approch,
I did:

Open "C:\myfolder\mytest.rtf" For Binary Access Read As fnum 
        txt = Input(LOF(fnum), #fnum) 
Close fnum

1)
Is your Get  more suitable then Input?

2)
Is Binary faster then Output?


3)
To save I go with:

   Open "C:\myfolder\mytest.rtf" For Output As fnum 
            Print #fnum, txt
   Close fnum

is:

  Open "C:\myfolder\mytest.rtf" For Binary Access Write As fnum

             Put #fnum, , txt & stringNewAddition & stringHeaderClosing
Close fnum

faster?



4)
I need to perform some parsing, so I need the tags.
I'm trying to write some rtf formatted text into a .rtf file,
without using the Richtx32.ocx control,
because I know it has W98 and WXP versioning problems.

I mix a preexisting .rtf file with some rtf text that my app prepares.
I'm using the RTF Specs to prepare the text.
Do you know any way (in example by code) to manage .rtf text,
without using the Richtx32.ocx control ?
I mean like some .bas module available from many VB developers' sites
that can fully replace the ComDlg32.ocx file to show the
common dialgos windows?













Show quoteHide quote
>> I need to load the whole textual content
>> (with tags and so on...)
>> of a .rtf file into a String variable.
>>
>> Like this:
>> myVar = "{\rtf1\ansi\ansicpg1252\deff0\deflang1040\def..."
>>
>> When I try to read a .rtf file
>> previously saved by 'MS WordPad'
>> the LOF instruction does fail.
>
>This code will do what you want...
>
>   Dim mytxt As String
>   Dim fnum As Integer
>   fnum = FreeFile
>   Open "C:\myfolder\mytest.rtf" For Binary As fnum
>     mytxt = Space(LOF(fnum))
>     Get fnum, , mytxt
>   Close fnum
>
>I have a question for you though... Why are you doing this? Is the ultimate
>aim to put the text loaded into the 'mytxt' variable into a RichTextBox? If
>so, why not simply load the file directly into the RichTextBox like this...
>
>   RichTextBox1.LoadFile "C:\myfolder\mytest.rtf"
>
>If you still need the file stored in a variable, you can load it up from the
>RichTextBox itself like this...
>
>   mytxt = RichTextBox1.TextRTF
>
>Rick
>
>
Author
31 Jan 2006 12:19 PM
J French
On Tue, 31 Jan 2006 01:25:27 GMT, And***@inwind.it (AndyIT) wrote:

>Yes, I've read the previous post and I used the Binary approch,
>I did:
>
>Open "C:\myfolder\mytest.rtf" For Binary Access Read As fnum 
>        txt = Input(LOF(fnum), #fnum) 
>Close fnum
>
>1)
>Is your Get  more suitable then Input?

Input should fail on Chr$(26)
- much safer to use Get

>2)
>Is Binary faster then Output?

They are different things, when you open a file for Output you zap its
length which definitely takes time
- but if you want to zap its length, then Output is sensible

>3)
>To save I go with:
>
>   Open "C:\myfolder\mytest.rtf" For Output As fnum 
>            Print #fnum, txt
>   Close fnum

You are adding an extra CrLf
-  Print# fnum, Txt ;      <--- note the semi colon

>is:
>
>  Open "C:\myfolder\mytest.rtf" For Binary Access Write As fnum
>
>             Put #fnum, , txt & stringNewAddition & stringHeaderClosing
> Close fnum
>
>faster?

That will be faster, but if the data written is shorter than the
length of the existing file you will have garbage left over

You might want to look at 'Append'

Show quoteHide quote
>
>4)
>I need to perform some parsing, so I need the tags.
>I'm trying to write some rtf formatted text into a .rtf file,
>without using the Richtx32.ocx control,
>because I know it has W98 and WXP versioning problems.

>I mix a preexisting .rtf file with some rtf text that my app prepares.
>I'm using the RTF Specs to prepare the text.
>Do you know any way (in example by code) to manage .rtf text,
>without using the Richtx32.ocx control ?
>I mean like some .bas module available from many VB developers' sites
>that can fully replace the ComDlg32.ocx file to show the
>common dialgos windows?

Use the GetOpenFileName API to replace that horrible OCX

For an example get the downloadable API Guide (packed with examples)
from
http://www.mentalis.org/agnet/

You can also find an example at:

http://vbnet.mvps.org/

Look for Randy Birch's stuff
Author
31 Jan 2006 10:01 PM
Jeff Johnson [MVP: VB]
"J French" <erew***@nowhere.uk> wrote in message
news:43df53c6.266103867@news.btopenworld.com...

> You can also find an example at:
>
> http://vbnet.mvps.org/
>
> Look for Randy Birch's stuff

??? vbnet.mvps.org IS Randy Birch's stuff. Not much "looking" required....
Author
1 Feb 2006 11:01 AM
J French
On Tue, 31 Jan 2006 17:01:51 -0500, "Jeff Johnson [MVP: VB]"
<i.get@enough.spam> wrote:

>
>"J French" <erew***@nowhere.uk> wrote in message
>news:43df53c6.266103867@news.btopenworld.com...
>
>> You can also find an example at:
>>
>> http://vbnet.mvps.org/
>>
>> Look for Randy Birch's stuff

>??? vbnet.mvps.org IS Randy Birch's stuff. Not much "looking" required....

Oops
I was thinking about his old site at http://www.mvps.org/