Setting Directories and the If Len(Dir(… statement

2020-03-31 04:29发布

I have a file exists under this path:

//path/folder1/folder2/datafile.gdp

It is an input to an external program being called from vba in this manner:

Sub RunProgram()
Dim wsh As Object
SetCurrentDirectory "\\path\"
ChDir "\\path\folder1\folder2\" 'Set Directory
Set wsh = VBA.CreateObject("WScript.Shell")
check = CurDir
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

But on the final line, including \path\folder1\folder2\ before the input file name appears to cause the external program to want to write some files into a duplicated directory that doesn't exist, causing an error. It used to work in this format before the .exe was updated by an external company. It now wants to write some files here, all prefixed with the name of my input file:

\\path\folder1\folder2\PATH\FOLDER1\FOLDER2\

Hoping to fix this, I changed the final line of the code to this, following some comments on a previous post here on SO:

Statusnum = wsh.Run(Command:="program.exe ""datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

Since the directory is set correctly prior to calling the .exe, I thought removing the path for the input file would solve the issue.

The program now launches, but doesn't appear to load the input file with it and no longer runs calculations automatically in the background as it should. Instead, it launches and the main .exe window pops up to the user as if it had just been launched for setting up a new project, calculations don't occur automatically.

To check which directory the VBA code was try to pull my datafile.gdp from, I created these loops directly before calling the .exe:

If Len(Dir("\\path\folder1\folder2\datafile.gdp")) = 0 Then
    FileIsMissing1 = True 'I use Excel-VBA watches to break if true
End If

If Len(Dir("datafile.gdp")) = 0 Then
    FileIsMissing2 = True 
End If

Bizarrely, neither of these loops causes a break. The file only exists in

\\path\folder1\folder2\datafile.gdp

Not in the duplicated directory... so why are both of these statements satisfied? Does entering the directory make no difference even when the current directory has been set? The current directory seems to be impacting the line:

Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

But not these if loops, and I'm not sure why.

0条回答
登录 后发表回答