Results 1 to 9 of 9

Thread: [RESOLVED] Execute a file via a command button

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Execute a file via a command button

    Hi guys and girls,

    I have saved some registry settings as a .reg file using the method similar to the way you would write a text file.

    The file is called backup.reg.

    What I am looking to do is using a command button. The reg file is going to be located on a floppy disk. When the command button is clicked, I would like to execute the .reg file as if you were going into the floppy disk and double clicking on it to execute.

    I would like to know if this is possible and if you can provide any samples. Have searched VBF without success so far. Thanks in advance.

  2. #2
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Execute a file via a command button

    Shell "A:\Filename.reg" ?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Execute a file via a command button

    Quote Originally Posted by Spajeoly
    Shell "A:\Filename.reg" ?
    already tried that and get the following error:
    Run-time error '5':
    Invalid procedure call or argument.

  4. #4
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Execute a file via a command button

    try this

    VB Code:
    1. Private Declare Function FindExecutable _
    2.     Lib "shell32.dll" Alias "FindExecutableA" _
    3.     (ByVal lpFile As String, _
    4.     ByVal lpDirectory As String, _
    5.     ByVal lpResult As String) As Long
    6.  
    7. Private Sub Form_Load()
    8.  
    9. runapp "C:\test.reg", "reg"
    10.  
    11. End Sub
    12. Public Function runapp(strname As String, appname As String) As Long
    13.  
    14.     Dim strResult As String
    15.     Dim lngResult As Long
    16.  
    17.         strResult = String(255, 0)
    18.         lngResult = FindExecutable(strname, vbNullString, strResult)
    19.         strResult = Trim(Replace(strResult, "/dde", "", 1))
    20.         'run the file and not an .exe file
    21.         i = Shell(Trim(Replace(strResult, vbNullChar, "", 1)) & " " & strname, 1)
    22. End Function
    Chris

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Execute a file via a command button

    actually, all you need is

    VB Code:
    1. Shell "regedit.exe c:\test.reg"
    Chris

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Execute a file via a command button

    this code works perfectly!!

    Quote Originally Posted by the182guy
    try this

    VB Code:
    1. Private Declare Function FindExecutable _
    2.     Lib "shell32.dll" Alias "FindExecutableA" _
    3.     (ByVal lpFile As String, _
    4.     ByVal lpDirectory As String, _
    5.     ByVal lpResult As String) As Long
    6.  
    7. Private Sub Form_Load()
    8.  
    9. runapp "C:\test.reg", "reg"
    10.  
    11. End Sub
    12. Public Function runapp(strname As String, appname As String) As Long
    13.  
    14.     Dim strResult As String
    15.     Dim lngResult As Long
    16.  
    17.         strResult = String(255, 0)
    18.         lngResult = FindExecutable(strname, vbNullString, strResult)
    19.         strResult = Trim(Replace(strResult, "/dde", "", 1))
    20.         'run the file and not an .exe file
    21.         i = Shell(Trim(Replace(strResult, vbNullChar, "", 1)) & " " & strname, 1)
    22. End Function

  7. #7
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Execute a file via a command button

    VB Code:
    1. 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
    2.  
    3. Private Sub Command1_Click()
    4. ShellExecute Me.hwnd, "open", "a:\backup.reg", 0&, 0&, 1
    5. End Sub

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Execute a file via a command button

    this code works too and will use this as its less code than the other one. either way, both work great. thanks for the help.

    Quote Originally Posted by the182guy
    actually, all you need is

    VB Code:
    1. Shell "regedit.exe c:\test.reg"

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Execute a file via a command button

    wow, works too. now have to choose which to use. so many choices. thansk guys.

    Quote Originally Posted by Jmacp
    VB Code:
    1. 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
    2.  
    3. Private Sub Command1_Click()
    4. ShellExecute Me.hwnd, "open", "a:\backup.reg", 0&, 0&, 1
    5. End Sub

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