Robert English
Robert English

Reputation: 223

Open txt file, create new txt file, save old text to new file

I'm looking for code that prompts an OpenFile box for an existing txt file (file1), then create a new file and prompts SaveAs box for the new txt file (file2). The contents of file1 to be saved in file2 later.

So far Ive gotten to first part to work fine. I know the second part is wrong but I don't know what to do.

My code so far;

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

Upvotes: 0

Views: 1375

Answers (1)

d-stroyer
d-stroyer

Reputation: 2706

The line

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

looks quite wrong indeed.

You coud try this instead to pop-up a dialog for saving file

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

Update : The full code is

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

Upvotes: 2

Related Questions