2017-09-14 31 views
0

我正在VB.Net中製作一個簡單的應用程序,允許用戶選擇一個文件,然後它將格式化並將文件放在正確的目的地,從而使重複的過程少得多乏味。我使用Excel在VBA中工作,但我寧願有我自己的獨立應用程序。我不需要Excel來以任何方式操縱信息。但是,當我點擊「執行」按鈕時會產生錯誤。 因此,這是對我的按鈕,工作代碼:ArgumentException複製文件時未處理

Private Sub executor_Click(sender As Object, e As EventArgs) Handles executor.Click 
    Dim thisDate As String, myFile As String, toPath As String, FSO As Object, fFormat As String 
    myFile = nameInput.ToString 
    thisDate = Format(Now(), "yyyymmdd") 
    toPath = "C:\Test\" 
    fFormat = "AQDOS" & myFile & thisDate & ".pdf" 

    FSO = CreateObject("scripting.filesystemobject") 

    FSO.CopyFile(Source:=sFileSelected, Destination:=toPath & fFormat) 

所以它突出FSO.CopyFile(Source:=sFileSelected, Destination:=toPath & fFormat)並說該異常是未處理的。 'sFileSelected'是一個公共變量,其值在不同的子程序中計算。我不知道這是問題的核心還是不是,但不管出於何種原因,它都不喜歡最後一行。

是我試圖將字符串追加到名稱上的問題?

編輯:很明顯,問題存在與源,因爲現在我有代碼正確執行格式。所以我的問題是,我如何引用一個由其他button_Click定義的變量?

回答

0

嘗試重寫你的最後一行,並通過在純參數:

FSO.CopyFile(sFileSelected, toPath & fFormat) 

但我更喜歡你在完成傳遞variavbles不是將它們連接在參數功能清晰的代碼:

dim string as fileDestination = toPath & fFormat 
FSO.CopyFile(sFileSelected, fileDestination