Results 1 to 6 of 6

Thread: Assign a text file from hard disk into a VBA string variable

  1. #1
    Join Date
    Jun 2011
    Posts
    85

    Assign a text file from hard disk into a VBA string variable

    I need your help. Recently I want to read a short text string from hard disk and assign into a VBA string variable. I have done research over the internet with the help of my browser and found no help. Actually I don’t want to open a file and put it directly on to a worksheet and then read, last close the worksheet. It is difficult for a four character long file. Please provide necessary syntax or code for it.

  2. #2
    Join Date
    Apr 2009
    Posts
    488

    Re: Assign a text file from hard disk into a VBA string variable

    I can understand your query that you have posted. You need a function for binary access. See the below functions:


    Code:
    Sub ReadFile()
    Dim hFile As Long
    Dim strFile As String
    Dim strData As String * 4
    
    hFile = FreeFile
    strFile = "C:\Something.txt"
    Open strFile For Binary Access Read As hFile Len = 4
    Get hFile, 1, strData
    Close hFile
    MsgBox strData
    End Sub
    Also you can read line the file system object library.

  3. #3
    Join Date
    May 2009
    Posts
    539

    Re: Assign a text file from hard disk into a VBA string variable

    I have tried the below functions by myself so I can assure you that it works fine with me and so it will be for you. The function will read in a multi line file where each line is enclosed by a NL/LF pair.

    Code:
    Sub ReadMultiLines()
    'requires a reference to the Windows Script Host Object Model
    Dim FSO As FileSystemObject
    Dim tsInput As TextStream
    Dim strFile As String
    Dim strLine As String
    
    strFile = "D:\DspfxId.txt"
    Set FSO = New FileSystemObject
    Set tsInput = FSO.OpenTextFile(strFile, 1)
    
    Do While Not tsInput.AtEndOfStream
    
    strLine = tsInput.ReadLine
    Debug.Print strLine
    
    Loop
    tsInput.Close
    Set FSO = Nothing
    End Sub

  4. #4
    Join Date
    Apr 2009
    Posts
    569

    Re: Assign a text file from hard disk into a VBA string variable

    The below coding will give you output such as.
    Produced:
    • this is line 1
    • This is line 2
    • This is line 3
    • This is line 4


    These outputs are in the text file. The code is:

    Code:
    Sub Tester4()
    Dim fname As String
    Dim sVal As String
    fname = "C:\xlText\MyText.txt"
    sVal = OpenTextFileToString2(fname)
    Debug.Print sVal
    End Sub
    
    Function OpenTextFileToString2(ByVal strFile As String) As String
    ' RB Smissaert - Author
    Dim hFile As Long
    hFile = FreeFile
    Open strFile For Input As #hFile
    OpenTextFileToString2 = Input$(LOF(hFile), hFile)
    Close #hFile
    End Function

  5. #5
    Join Date
    May 2009
    Posts
    529

    Re: Assign a text file from hard disk into a VBA string variable

    To read the file you have to use the below code in your source code. Its very easy to understand if you read it carefully.

    Code:
    public string ReadFromFile(string filename)
    {
    StreamReader sr = File.OpenText(filename);
    
    StringBuilder sb = new StringBuilder();
    string str = sr.ReadLine();
    while( str != null )
    {
    sb.Append(str + "\n");
    str = sr.ReadLine();
    }
    sr.Close();
    return sb.ToString();
    }

  6. #6
    Join Date
    May 2009
    Posts
    637

    Re: Assign a text file from hard disk into a VBA string variable

    Add this code to read from the directory directly.

    Code:
    StreamReader SR;
    string S;
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null) {
    Console.WriteLine(S);
    S=SR.ReadLine();
    }
    SR.Close();

Similar Threads

  1. Assign each line of a text file to a variable
    By hitman126 in forum Operating Systems
    Replies: 1
    Last Post: 22-01-2011, 07:42 AM
  2. How to read string from a given text file using C# program?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:18 PM
  3. Replace text string using batch file
    By jean-paul martell in forum Operating Systems
    Replies: 2
    Last Post: 23-06-2009, 01:18 PM
  4. Reading text string from text file (PHP)
    By Moderate in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 03:27 PM
  5. Replies: 1
    Last Post: 10-12-2008, 12:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,811,756.86588 seconds with 16 queries