2013-07-12 67 views
0

我正在尋找代碼來提示現有txt文件(file1)的OpenFile框,然後創建一個新文件並提示SaveAs框爲新txt文件(file2)。稍後將file1的內容保存在file2中。打開txt文件,創建新的txt文件,將舊文本保存到新文件中

到目前爲止我已經得到了第一部分工作正常。我知道第二部分是錯誤的,但我不知道該怎麼做。

我的代碼到目前爲止;

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

相信我,我已經試過了。我不斷變得愚蠢的'暴露預期'的錯誤,我不知道爲什麼!它當然應該與打開的文件命令具有相同的格式嗎? –

+0

哪一行會引發「預期表達式」錯誤? –

+0

您建議的那個 –