vba 讀取文件



主要是用open 方法讀取除txt以外的文件,讀出來的數據是亂碼的,還需要理解一下。


Sub readFileByChoose()
    Dim fileName As String
    'Dim sFileName As String
    'Dim aFile As Variant
   
    fileName = getFilePath()
   
    If fileName <> "" Then
        'Range("B3") = fileName
       
        'aFile = Split(fileName, "\")
        'sFileName = aFile(UBound(aFile))
       
        'Call createFile(fileName, sFileName)
        Call createFile(fileName)
    End If
End Sub

Function getFilePath() As String
    Dim name As Variant
    name = Application.GetOpenFilename("All File,*.*")
    If name <> False Then
        getFilePath = name
    End If
End Function

Function createFile(fileName As String)
    Dim txt As String
    Open fileName For Input As #1
    Open "D:\aaaaa.docx" For Output As #2
    Do While Not EOF(1)
        Line Input #1, txt
        MsgBox txt
        Write #2, txt
    Loop
    Close
End Function

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章