How to Open a RTF file in VBScript and read text from it.

Posted on 2009-02-09
Last Modified: 2012-08-13
I am using VBScript in which I want to open a RTF file and read text from it.

Please provide a solution to this problem.

Question by:pranjal_ds
    LVL 12

    Assisted Solution

    Hi There

    This is a way to open an RTF file and read each line one by one, though you can read the whole file in one go if you like or even character by character (but thats not so common)

    See if it helps


    P.S> change teh filename to suit you and see the comments as to where to do some things with each line we read.
    	Call ReadFile
    Sub ReadFile()
     ' Version 1.0
     ' Written by: Krystian Karia
     ' Dated: 22-12-2008
     	Dim fso, oFile
    	Dim stroutput, arrComputers
    	Dim strResults
    	Dim tf
    	Const ForReading = 1
    	Const sFileLocation = "C:\Filename.rtf" ' <_______Change to suit
    	Set fso = CreateObject("Scripting.FileSystemObject")
    	Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
    		If Err.Number <> 0 Then
    			WScript.Echo "Error reading File " & sfileLocation
    			Exit Sub
    		End If
    ' Read the whole contents of the File
    	Do While oFile.AtEndOfStream <> True
    		strResults = oFile.ReadLine
    ' *******  Do some stuff here with the line we just read  *******
    		WScript.Echo strResults
    ' Close the file
    ' Release the object from memory
    	Set oFile = Nothing
     End Sub 

    Open in new window


    Accepted Solution

    Ok Thank you...!

    Author Comment

    Solution was ok.

    Author Comment

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
    Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now