2017-09-14 75 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