EXCEL VBA读/写。 dft文件(IBM Data Tranfer)

时间:2017-03-08 17:25:23

标签: excel-vba ibm-midrange vba excel

我想通过VBA编辑.dtf(IBM数据传输文件)。在excel中打开它会导致我出现问题所以我希望逐行读取文件并导出到新的.dtf。但是当我尝试将Print #1字符串添加到.dtf

时出现问题

INPUT

OUPUT

Sub test()
    Dim TemplateFilePath As String
    TemplateFilePath = "C:\TemplateFile.dtf"
    Call CreateDataTranderFile(TemplateFilePath)
End Sub



Public Sub CreateDataTranderFile(TemplateFilePath As String)
    Dim output As String
    Dim OutFullPath As String
    Dim Line As String
    Dim FileNum As Integer

    OutFullPath = "C:\tmpTranferfile.dtf"


    'Read from template
    FileNum = FreeFile()
    Open TemplateFilePath For Input As #FileNum
        While Not EOF(FileNum)
            Line Input #FileNum, Line
            output = output & Line
        Wend
    Close #FileNum

    'Write .dtf
    Open OutFullPath For Output As #1
        Print #1, output
    Close
End Sub

1 个答案:

答案 0 :(得分:2)

因此,我假设您的输入文件是UNICODE文件,然后您可以使用以下函数

Function ReadUniCodeTextFile(inpFile As String) As String

Dim sText As String
Dim objFSO As Object
Dim objFile As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(inpFile, 1, False, -1)
    ReadUniCodeTextFile = objFile.ReadAll
    objFile.Close

End Function

并替换

   'Read from template
    FileNum = FreeFile()
    Open TemplateFilePath For Input As #FileNum
        While Not EOF(FileNum)
            Line Input #FileNum, Line
            output = output & Line
        Wend
    Close #FileNum

只需

output = ReadUniCodeTextFile(TemplateFilePath)

所以你的代码应该是这样的

Public Sub CreateDataTranderFile(TemplateFilePath As String)
Dim output As String
Dim OutFullPath As String
Dim Line As String
Dim FileNum As Integer

    OutFullPath = "C:\tmpTranferfile.dtf"

    'Read from template
    output = ReadUniCodeTextFile(TemplateFilePath)

    'Write .dtf
    Open OutFullPath For Output As #1
    Print #1, output
    Close
End Sub