打开txt文件,创建新的txt文件,将旧文本保存到新文件

时间:2013-07-12 11:28:42

标签: vba excel-vba excel

我正在寻找提示现有txt文件(file1)的OpenFile框的代码,然后创建一个新文件并提示新txt文件(file2)的SaveAs框。 file1的内容稍后将保存在file2中。

到目前为止,我已经完成了第一部分工作。我知道第二部分是错的,但我不知道该怎么做。

到目前为止我的代码;

infilename$ = Application.GetOpenFilename("Neutral Files (*.txt),*.txt", , "Open
Neutral File", "OPEN", False)
If infilename$ = "False" Then
    msg = MsgBox("No input file selected. Press OK to retry or cancel to quit",vbOKCancel)
    If msg = vbOK Then
        Do While msg <> vbCancel  'loop until user presses cancel
            infilename$ = Application.GetOpenFilename("Neutral Files (*.r01),*.r01", , "Open Neutral File", "OPEN", False)
            If infilename$ = "False" Then
                msg = MsgBox("No input file selected. Press OK to retry or cancel to quit", vbOKCancel)
            End If
        Loop
    ElseIf msg = vbCancel Then Exit Sub
    End If
End If

outfilename$.SaveAs =:"FileName"infilename.txt",False"

If outfilename$ = "False" Then
    msg = MsgBox("No output file selected. Press OK to retry or cancel to quit", vbOKCancel)

    If msg = vbOK Then
        Do While msg <> vbCancel  'loop until user presses cancel
            outfilename$ = Application.SaveAsFilename("Neutral Files (*.r01),*.r01", , "Save As Output", "SAVE", False)

            If outfilename$ = "False" Then
                msg = MsgBox("No output file selected. Press OK to retry or cancel to quit", vbOKCancel)
            End If
        Loop
    ElseIf msg = vbCancel Then Exit Sub
    End If
End If

1 个答案:

答案 0 :(得分:2)

该行

outfilename$.SaveAs =:"FileName"infilename.txt",False"

确实看起来很不对。您可以尝试这样做以弹出一个用于保存文件的对话框

outfilename$ = Application.GetSaveAsFilename(infilename$, "Neutral Files (*.txt),*.txt", , "Save file", "SAVE")

更新:完整代码是

Sub test()
Dim outfilename as Variant
Dim infilename as Variant
Dim msg As Variant
infilename = Application.GetOpenFilename("Neutral Files (*.txt),*.txt", , "Open Neutral File", "OPEN", False)
If infilename = "False" Then
    msg = MsgBox("No input file selected. Press OK to retry or cancel to quit", vbOKCancel)
    If msg = vbOK Then
        Do While msg <> vbCancel  'loop until user presses cancel
            infilename = Application.GetOpenFilename("Neutral Files (*.r01),*.r01", , "Open Neutral File", "OPEN", False)
            If infilename = "False" Then
                msg = MsgBox("No input file selected. Press OK to retry or cancel to quit", vbOKCancel)
            End If
        Loop
    ElseIf msg = vbCancel Then Exit Sub
    End If
End If

 outfilename = Application.GetSaveAsFilename(infilename, "Neutral Files (*.txt),*.txt", , "Save file", "SAVE")

If outfilename = "False" Then
    msg = MsgBox("No output file selected. Press OK to retry or cancel to quit", vbOKCancel)

    If msg = vbOK Then
        Do While msg <> vbCancel  'loop until user presses cancel
            outfilename = Application.SaveAsFilename(infilename, "Neutral Files (*.r01),*.r01", , "Save As Output", "SAVE")

            If outfilename = "False" Then
                msg = MsgBox("No output file selected. Press OK to retry or cancel to quit", vbOKCancel)
            End If
        Loop
    ElseIf msg = vbCancel Then Exit Sub
    End If
End If

End Sub