Results 1 to 19 of 19

Thread: Vb help using textbox to run executable file from a folder

  1. #1

    Thread Starter
    Registered User
    Join Date
    Aug 2015
    Posts
    6

    Vb help using textbox to run executable file from a folder

    Hello there!
    I'm a new bee in VB programming and really need to start with this project....

    I need to run executable file that are being input in a text box after clicking the execute button...

    Example:
    If i write file name in the text box... it must open directly after clicking the execute button...
    Only one containing folder will be the source of the files (example: excel file, word... etc)

    Please help...

    Thank You in advance

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Vb help using textbox to run executable file from a folder

    Code:
    Private Sub cmdExecute_Click()
     Shell "path-to-folder\" & Text1.Text, vbNormalFocus
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Aug 2015
    Posts
    6

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by jmsrickland View Post
    Code:
    Private Sub cmdExecute_Click()
     Shell "path-to-folder\" & Text1.Text, vbNormalFocus
    End Sub
    Hello Sir,
    Thank's for your reply...
    I already tried the codes but encountered some error..
    Name:  b.jpg
Views: 993
Size:  32.6 KB

    "a" is the sample file name located in drive C....
    Name:  a.jpg
Views: 1175
Size:  20.0 KB

    Again thank you....

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb help using textbox to run executable file from a folder

    Are you using a .Net product of Visual Basic, or Visual Basic 6.0?

    If the former (as I suspect), I can ask a Moderator to move this thread to the .Net section of the Forum.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb help using textbox to run executable file from a folder

    Yeah, pretty sure it is .Net....but I noticed you are using a literal string "path-to-folder", rather than a string variable, like path-to-folder.
    Or, in other words, you need to assign whatever path you want to the variable, like path-to-folder, and when you shell it, don't use the quotes.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Vb help using textbox to run executable file from a folder

    Well, except that there are more quotes required that are not even shown there. If the built-up string eventually passed to the call has a space it all falls over otherwise.

    That's true whether you are really using VB or the Great Pretender VB.Net.

    So in VB6 more like:

    Code:
    Private Sub cmdExecute_Click()
     Shell """path-to-folder\" & Text1.Text & """", vbNormalFocus
    End Sub

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Vb help using textbox to run executable file from a folder

    Looks like since OP only types in the name (as in his post #3) then OP will need to add the .exe or it needs to be added for him in the command button sub

    Code:
    Private Sub cmdExecute_Click()
     Shell """C:\" & Text1.Text & ".exe""", vbNormalFocus
    End Sub
    Just to ensure whether the .exe is included or not included in the textbox you might want to use below instead

    Code:
    Private Sub cmdExecute_Click()
     If Right(Text1.Text, 4) = ".exe" Then
       Shell """C:\" & Text1.Text & """", vbNormalFocus
     Else
       Shell """C:\" & Text1.Text & ".exe""", vbNormalFocus
     End If
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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

    Re: Vb help using textbox to run executable file from a folder

    It is not required that the exe be added to the filename, unless of course there are other files by that same name in the given folder with different extensions.

    Shell "c:\MyFile" would execute MyFile.Exe if it were present or MyFile.Bat if that were present or myFile.Com if present . If all three were present I have no idea which it would execute

  9. #9
    Lively Member
    Join Date
    Apr 2015
    Posts
    120

    Re: Vb help using textbox to run executable file from a folder

    Between .EXE and .BAT , .EXE comes first.

    In DOS’s days , (if I’m not mistaken) , first option was the .COM one.

  10. #10

    Thread Starter
    Registered User
    Join Date
    Aug 2015
    Posts
    6

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by jmsrickland View Post
    Looks like since OP only types in the name (as in his post #3) then OP will need to add the .exe or it needs to be added for him in the command button sub

    Code:
    Private Sub cmdExecute_Click()
     Shell """C:\" & Text1.Text & ".exe""", vbNormalFocus
    End Sub
    Just to ensure whether the .exe is included or not included in the textbox you might want to use below instead

    Code:
    Private Sub cmdExecute_Click()
     If Right(Text1.Text, 4) = ".exe" Then
       Shell """C:\" & Text1.Text & """", vbNormalFocus
     Else
       Shell """C:\" & Text1.Text & ".exe""", vbNormalFocus
     End If
    End Sub
    Hello Sir....

    Thank you all for the inputs, but still same error occurs...
    Here's the start of the string...
    Im using Microsoft Visual Studio 2010, Visual Basic Windows Form Application...
    Name:  4.jpg
Views: 850
Size:  19.2 KB

  11. #11

    Thread Starter
    Registered User
    Join Date
    Aug 2015
    Posts
    6

    Re: Vb help using textbox to run executable file from a folder

    I've already tried also in VB6 but still i encountered same error...
    Name:  6.jpg
Views: 935
Size:  17.6 KB

    Name:  7.jpg
Views: 931
Size:  20.8 KB

    Example...

    I have a JPG file on drive C:\ which i need to open using this program...

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb help using textbox to run executable file from a folder

    Did you not try similar code I gave you in your PM?

    If your jpg file is a the root directory of C:\ (which is NOT a good idea as most people won't have permissions to write to the root of C.)
    And, I don't know how to SHELL a jpg file. What do you want to open the JPG file WITH? What program? Shell THAT program and pass the jpg as a parameter if it takes one.

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

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by Larome View Post
    I've already tried also in VB6 but still i encountered same error...

    Example...

    I have a JPG file on drive C:\ which i need to open using this program...
    jpg is not an executable. You said you wanted to run an executable now you say you want to open a jpg which of course is not an executable.

    File not found means that the contents of your text box is not a valid path\filename or the file does not exist

    To open a file with an executable then you either have to shell the executable that opens the file and pass the filename as a parameter or you need to use the API

  14. #14

    Thread Starter
    Registered User
    Join Date
    Aug 2015
    Posts
    6

    Re: Vb help using textbox to run executable file from a folder

    Hello Sir SamOscarBrown

    Good day.
    I've already tried your codes and it was very helpful...
    Actually im running the program now and having good progress but encountered new problems,,,

    i'm using barcode reader to write on the textbox,,,,

    Example...
    after scanning... the textbox will have ASDFGHJKL... total 9 letters
    but in folder i only have FGH.xlsx
    so it will nut run....
    how can i execute the file... i only need the number 4,5 and 6 letters...

  15. #15
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by dilettante View Post
    Well, except that there are more quotes required that are not even shown there. If the built-up string eventually passed to the call has a space it all falls over otherwise.

    That's true whether you are really using VB or the Great Pretender VB.Net.

    So in VB6 more like:

    Code:
    Private Sub cmdExecute_Click()
     Shell """path-to-folder\" & Text1.Text & """", vbNormalFocus
    End Sub
    On Windows Vista & 7, it appears that the intrinsic Shell function actually doesn't fail if the passed filename contains spaces yet isn't surrounded by quotes.

    The documentation of the Shell function, however, warned against not using quotes:

    Quote Originally Posted by MSDN
    Security Note If you do not enclose the path and file specification in quotes, there is a security risk if the file name or a path node contains spaces. If the path node specification is not inside quotes, for example \Program Files and a program named Program.exe had been installed in C:\, for example by illicit tampering, Windows would execute it instead of MyFile.exe.
    Quote Originally Posted by DataMiser View Post
    It is not required that the exe be added to the filename, unless of course there are other files by that same name in the given folder with different extensions.

    Shell "c:\MyFile" would execute MyFile.Exe if it were present or MyFile.Bat if that were present or myFile.Com if present . If all three were present I have no idea which it would execute
    On Windows Vista & 7, if the filename passed to the Shell function doesn't have an extension, the function will raise a "File not found" error even if there is a file in the specified path that has the same name but with a .COM or .BAT extension. The reason for this is because on Windows Vista & 7, the Shell function apparently calls the CreateProcessW API function with the PathName parameter being passed to the lpCommandLine parameter and NULL (vbNullString) for the lpApplicationName parameter. According to CreateProcessW's documentation:

    Quote Originally Posted by MSDN
    If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is .com, this parameter must include the .com extension.
    Quote Originally Posted by JackB View Post
    Between .EXE and .BAT , .EXE comes first.

    In DOS’s days , (if I’m not mistaken) , first option was the .COM one.
    According to the quick help of the command interpreter's (cmd.exe) START command:

    Code:
    When executing a command line whose first token does NOT contain an
        extension, then CMD.EXE uses the value of the PATHEXT
        environment variable to determine which extensions to look for
        and in what order.  The default value for the PATHEXT variable
        is:
    
            .COM;.EXE;.BAT;.CMD


    BTW, as hinted above, the Shell function is actually Unicode-aware on Windows Vista & 7 (and possibly on newer OSs as well; haven't checked yet). Here's some test code for those who would like to verify it for themselves:

    Code:
    Option Explicit 'In a standard Module
    
    Private Type OPENFILENAME
        lStructSize       As Long
        hWndOwner         As Long
        hInstance         As Long
        lpstrFilter       As String
        lpstrCustomFilter As String
        nMaxCustFilter    As Long
        nFilterIndex      As Long
        lpstrFile         As String
        nMaxFile          As Long
        lpstrFileTitle    As String
        nMaxFileTitle     As Long
        lpstrInitialDir   As String
        lpstrTitle        As String
        Flags             As Long
        nFileOffset       As Integer
        nFileExtension    As Integer
        lpstrDefExt       As String
        lCustData         As Long
        lpfnHook          As Long
        lpTemplateName    As Long
    End Type
    
    Private Declare Function GetCommandLineW Lib "kernel32.dll" () As Long
    Private Declare Function GetOpenFileNameW Lib "comdlg32.dll" (ByVal lpOFN As Long) As Long
    Private Declare Function MessageBoxW Lib "user32.dll" (ByVal hWnd As Long, ByVal lpText As Long, ByVal lpCaption As Long, ByVal uType As VbMsgBoxStyle) As VbMsgBoxResult
    
    Private Sub Main()
        Const MAX_PATH = 260&, OFN_HIDEREADONLY = &H4&, OFN_FILEMUSTEXIST = &H1000&
        Dim sBaseFileName As String, OFN As OPENFILENAME
    
        MessageBoxW 0&, GetCommandLineW, StrPtr(App.Title), vbInformation
    
        With OFN
            .lStructSize = LenB(OFN)
            .lpstrFile = String$(MAX_PATH - 1&, 0)
            .nMaxFile = MAX_PATH
            .Flags = OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST
    
            If GetOpenFileNameW(VarPtr(OFN)) Then
                sBaseFileName = Mid$(.lpstrFile, .nFileOffset + 1&, .nFileExtension - .nFileOffset - 1&)
                Shell sBaseFileName & "   /Arg   /Param   /Switch", vbNormalFocus
            End If
        End With
    End Sub
    Compile that code and then give the executable a new Unicode filename, say:

    Code:
    日本人     العربية     русский     עברית.exe
    To verify that on Windows Vista & 7, the Shell function doesn't rely on the PATHEXT environment variable when the specified file's extension is missing, create 2 copies of the EXE and then rename them so that they have the same base name as the original file but with .COM and .BAT extensions instead. Run any of the 3 executable files and observe their command lines. Notice that no matter which one is chosen in the Open File dialog, the Shell function always launches the EXE file, provided it is present.
    Last edited by Bonnie West; Aug 7th, 2015 at 03:23 PM. Reason: Added Vista
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by Larome View Post

    Example...
    after scanning... the textbox will have ASDFGHJKL... total 9 letters
    but in folder i only have FGH.xlsx
    so it will nut run....
    how can i execute the file... i only need the number 4,5 and 6 letters...
    Sam already told you (post 12) that you need to execute the program that it in turns runs the .xlsx file and pass to that program the name of the file as a argument

    For example if you wanted to open a bitmap file you would code something like this:

    Shell """C:\Windows\system32\mspaint.exe """ & """The path to where the bitmap is\MyImage.bmp"""", vbNormalFocus"

    Make sure you put a space after the .exe. Also make sure your quot marks are correct

    As far as getting the 4th, 5th, and 6th letters from your textbox you can use the Mid$ function like this
    FileName = Mid$(Text1.Text, 4, 3) & ".xlsx"

    then use the variable FileName in the Shell function like this:

    Shell """Path to where the program is that runs xlsx files """ & """The path to where the xlxs file is\" & FileName, vbNormalFocus

    Keep in mind this example is only for the example you are showing and you probably will need to create your own code if the positions of the letters change

    Now, let's say you do not know the path of the executable but you know the path of the file. In this case you can use ShellExecute instead of Shell. To use ShellExecute you must include the ShellExecute API in your code. For example, using the bitmap example let's say you do not know the location of mspaint.exe. ShellExecute will find the executable for you so all you need to do is the supply the path and file name

    Code:
    Private 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
     '
     '
    Private Command1_Click()
     Dim FileName As String
    
     FileName = "Path to where the bitmap is\" & "MyImage.bmp"
    
     ShellExecute 0, vbNullString, FileName, vbNullString, vbNullString, vbNormalFocus
    End Sub
    Above only works as long as Windows is aware of the default application that opens the requested file

    NOTE: I'm giving you code for VB6. I do not guarantee it for any version above VB6

    NOTE: Since you stated in your post #10 that you are using Microsoft Visual Studio 2010 you should really make your requests in the VB.Net Forum instead of here as this Forum is for VB6 and earlier


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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

    Re: Vb help using textbox to run executable file from a folder

    Quote Originally Posted by Bonnie West View Post


    In Windows 7, if the filename passed to the Shell function doesn't have an extension, the function will throw a "File not found" error even if there is a file in the specified path that has the same name but with a .COM or .BAT extension. The reason for this is because on Windows 7, the Shell function apparently calls the CreateProcessW API function with the PathName parameter being passed to the lpCommandLine parameter and NULL (vbNullString) for the lpApplicationName parameter. According to CreateProcessW's documentation:
    Interesting. I did not know this. I actually tested it under XP to confirm what I though was true and it worked fine without the extension.
    Of course when I write code I always include extensions so I never noticed there was a change in the later OS

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb help using textbox to run executable file from a folder

    Bonnie does come up with these tidbits all the time...obviously an experienced programmer (or just plain reads a lot!) :-)

  19. #19
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Vb help using textbox to run executable file from a folder

    Hard to say. Maybe she just looks up stuff when the time is right. Maybe she's a top notch software designer/developer at Microsoft. Who knows. I wouldn't say obviously an experienced programmer but I would say probably an experienced programmer


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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