| A routine to answer commonly asked newbie question. FileSystem Objects are avoided so that function will work with all VB versions Public Function ReadFileContents(FileFullPath As String) As _ String '****************************************************** 'Purpose: Opens a text file and returns its contents in a string variable 'Parameters: FileFullPath - Directory/FileName to open 'Returns: The contents of the file, or an empty string if an error occurs 'Example: 'sFileContents = ReadFileContents("C:\My Documents\MyFile.txt") '******************************************************* On Error GoTo ErrorHandler Dim iFileNumber As Integer Dim sAns As String If Dir(FileFullPath) = "" Then Exit Function iFileNumber = FreeFile Open FileFullPath For Input As #iFileNumber sAns = Input(LOF(iFileNumber), #iFileNumber) ReadFileContents = sAns ErrorHandler: Close #iFileNumber End Function |
Read Text From a File |
Express News India | Freelance ecommerce web development India