Results 1 to 9 of 9

Thread: Shell function syntax

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    Shell function syntax

    Hi

    I've done a forum search on this problem but no-one else seems to be having it. I have the following code

    Dim retVal
    retVal = Shell("C:\Readmems.exe", 1)

    But when I try and run the host program (And note that C:\Readmems.exe does exist) I just get the error

    "wrong number of arguments or invalid property assignment"

    What am I doing wrong ?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: Shell function syntax

    Are you really using VB6?

    When running your sample directly from the IDE
    Code:
    Private Sub Form_Load()
      Dim retVal
      retVal = Shell("C:\Readmems.exe", 1)
    End Sub
    Gives the following error:
    Code:
    Run-time error '53':
    
    File not found
    
    [Continue] [End] [Debug] [Help]

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    Re: Shell function syntax

    Quote Originally Posted by Arnoutdv View Post
    Are you really using VB6?
    Afraid so Up to now it has done everything I wanted so I didn't bother upgrading.

    What I have done is put a button (Called 'Shell') on my form and the full code is:

    Private Sub Shell_Click()
    Dim retVal
    retVal = Shell("""C:\Readmems.exe""")
    End Sub

    And I get that error.

    Just in case I tried it in Form_Load() but still got the same error

    Name:  Image1.jpg
Views: 122
Size:  33.2 KB

  4. #4
    Member
    Join Date
    Feb 2016
    Location
    France
    Posts
    32

    Re: Shell function syntax

    Hello, Why do you not use ShellExecute ?
    Code:
    ShellExecute (0, "Open", "C:\Readmems.exe", vbnullstring,vbnullstring,10)
    Last edited by christ62; Apr 6th, 2016 at 04:40 AM.

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,715

    Re: Shell function syntax

    Is it possible you have a function that is also named Shell? VB allows this, and I was able to reproduce your error like this:
    Code:
    Private Function Shell(a As String, b As Long, c As Long) As Long
    MsgBox "shelled"
    End Function
    Private Sub Command8_Click()
    Shell "C:\test.exe", 2, 3, 4
    End Sub
    Edit: Also I agree with the above poster that you should just switch to ShellExecute (or ShellExecuteEx), since VB's Shell has some issues on newer Windows versions (not the type that would cause your error here though)

    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Last edited by fafalone; Apr 6th, 2016 at 04:41 AM.

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: Shell function syntax

    The problem is that your command button is named "Shell" like the Shell function you want to call!
    Code:
    Private Sub Shell_Click()
    Dim retVal
    retVal = Shell("""C:\Readmems.exe""", 1)
    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    3

    Re: Shell function syntax

    Quote Originally Posted by Arnoutdv View Post
    The problem is that your command button is named "Shell" like the Shell function you want to call!
    Code:
    Private Sub Shell_Click()
    Dim retVal
    retVal = Shell("""C:\Readmems.exe""", 1)
    End Sub
    Oh, for goodness sake, how did I miss that ? I will crawl into a corner, assume the foetal position and suck my thumb.

    My only excuse is the quote "The only stupid question is the one you don't ask".

    Many thanks, although I will take the advice and start trying ShellExecute.

    Cheers

    Al

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell function syntax

    I would also suggest that you properly dim retval rather than letting it default to a variant.

  9. #9
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Shell function syntax

    Also I am nervous about doing some things in Form_Load
    I usually do them in Form_Activate, and have a flag to ensure they only happen once

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width