reading entire text file using vba

前端 未结 8 1279
暗喜
暗喜 2020-12-06 11:03

I\'m trying to read a text file using vba. I tried the below code

Open \"C:\\tester.txt\" For Input As #1
Worksheets(\"UI\").Range(\"H12\").Value = Input$(LO         


        
8条回答
  •  长情又很酷
    2020-12-06 11:12

    Fidel's answer, over Brettdj's answer, adjusted for ASCII or Unicode and without magical numbers:

    Public Function readFileContents(ByVal fullFilename As String, ByVal asASCII As Boolean) As String
        Dim objFSO As Object
        Dim objTF As Object
        Dim strIn As String
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTF = objFSO.OpenTextFile(fullFilename, IOMode:=ForReading, format:=IIf(asASCII, TristateFalse, TristateTrue))
        strIn = objTF.ReadAll
        objTF.Close
        readFileContents = strIn
    End Function
    

提交回复
热议问题