-1

I'm using VBA for MS Access in order to link a small C# app to my database as a helper tool. I have tried a couple of different ideas from stackoverflow itself, including the ShellAndWait utility and another on that page.

I have a button on a form. When you click this button, it should run another application that I am currently storing in %APPDATA%/program/

This is the code that is currently active:

Private Sub BtnImport_Click()

Dim file As String
Dim hProcess as Long

file = Environ("APPDATA") & "\program\component_import.exe"
'This is the standard version, which apparently does nothing at this time.
hProcess = Shell(file, vbNormalFocus)

'This is the RunApplication version I got from here earlier. It ends
'with "Successfully returned -532462766
import_funcs.RunApplication(file)

'This is the ShellAndWait version, which gives me a "File not Found" error
import_funcs.ShellAndWait(file, 0, vbNormalFocus, AbandonWait)

End Sub

I had changed the original shell out for both the ShellAndWait module and another similar module. Neither of those options work any differently in terms of my application not starting.

I have double-checked that "file" is correct (It points to C:\Users\Me\AppData\Roaming\program\component_import.exe). I have double-checked to make sure that my app is in the correct location.

It runs fine if I double-click from file explorer. It says Run-time error '53': File not found. whenever I attempt to run it from MS Access.

Any suggestions?

Edit: As an aside, the path itself does not contain any spaces.

Edit: Added some additional code. Link to first pastebin: RunApplication pastebin Link to second pastebin: ShellAndWait pastebin

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jim P
  • 534
  • 2
  • 8
  • 24
  • 2
    Is that a straight copy? Your filename seems to have gained a `)` and lost a `"`. – Andrew Leach May 03 '12 at 20:55
  • 1
    Tried your code with Notepad.exe. Can't start any executable from APPDATA via VBA (neither via file-explorer) - Win7 64bit as full admin - – Steve May 03 '12 at 21:21
  • @Andrew Leach: Yeah, I had not copied it because I had a bunch of extra comments from things that I was testing. Steve: When you run APPDATA from File explorer, you actually need to run it as %APPDATA%, since that is the Environment variable. – Jim P May 03 '12 at 21:24
  • 2
    If I move everything out of APPDATA (say C:\temp or C:\program files) your code works fine. Perhaps is something related to the security. I suppose that is not a good practice to start an executable from this folder. – Steve May 03 '12 at 22:05
  • After testing this on my home machine, it seems that it is a security related issue. The app works correctly at home. Additionally, it seems that moving the executable out of app_data seems to make things work without issue. – Jim P May 04 '12 at 11:14
  • So, I still have an issue of something not working correctly when running this script on my development machine. Is there any type of workaround for wonky security rules? – Jim P May 04 '12 at 12:41
  • It has sometimes been an issue with the different string storing ways between Visual Basic and C. I'm not sure how C# stores strings, but if it follows the zero-termination of C, you may have to convert the VB string into a C string before handing it over to the C# routine. – Christoph Jüngling Aug 08 '12 at 20:12

1 Answers1

0

I found sometimes folder names with spaces throws error when using shell command.

eg: C:\My Folder\appl.exe

make it: C:\MyFolder\appl.exe

Also can check for a valid path: The following code checks the folder where chrome.exe residing and calling www.google.com from there by passing url as argument:

Public Sub Display_Google()
  Dim chromePath As String
  chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"

  If FileExists(chromePath) Then
  Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus
  Else

  chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
  Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus
  End If
End Sub

Public Function FileExists(ByVal FileName As String) As Boolean
    On Error Resume Next
    FileExists = Not CBool(GetAttr(FileName) And (vbDirectory Or vbVolume))
    On Error GoTo 0
End Function
rchacko
  • 1,965
  • 23
  • 24