0

我正在寻找提示现有 txt 文件 (file1) 的 OpenFile 框的代码,然后创建一个新文件并提示 SaveAs 框输入新的 txt 文件 (file2)。稍后将 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
4

1 回答 1

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
于 2013-07-12T11:37:22.273 回答