Read whole content of rich text format file into string variable

G

Guest

Hi
I'd would like to read whole rich text format file (small file created in Ms Word) into a string variable, the code look like this

Dim intFileHander as intege
Dim strFileName as strin
Dim strContent as strin
..................................
strFileName="C:\temp\a.rtf
intFileHandler = FreeFil
Open strFileName For Output As intFileHandle
Input #intFileHandler, strConten
Debug.print strContent
Close #intFileHandle

Actually, strContent is storing only a first part of the input file. How can I read all content of my file into variable strContent

Thank
Tran Hong Quang
 
J

John Nurick

Hi,

The Input # statement parses the input data. Instead, use the Input()
function. Here's a UDF I use for this:

Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top