2013-07-12 4 views
0

Я ищу код, который запрашивает окно OpenFile для существующего файла txt (file1), затем создает новый файл и запрашивает окно SaveAs для нового txt-файл (файл2). Содержимое файла1 будет сохранено в файле2 позже.Открыть txt-файл, создать новый txt-файл, сохранить старый текст в новый файл

До сих пор Ive получил первую часть, чтобы нормально работать. Я знаю, что вторая часть ошибается, но я не знаю, что делать.

Мой код до сих пор;

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 

ответ

2

Линия

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

выглядит совершенно не так на самом деле.

Вы coud попробовать это вместо того, чтобы всплывающее диалоговое окно для сохранения файла

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 
+0

Поверьте мне, я пробовал. Я продолжаю получать глупые ошибки «Exprression Expected», и я не знаю, почему! Разумеется, он должен иметь тот же формат, что и команды открытого файла? –

+0

Какая строка выдает ошибку «Expression Expected»? –

+0

Тот, который вы предложили –

Смежные вопросы