vba 读取文件

来源:互联网 发布:淘宝卖家回评语大全 编辑:程序博客网 时间:2024/06/05 05:55


主要是用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

0 0