Results 1 to 2 of 2

Thread: Excel VBA: How To: Shell (Execute) thru a Shortcut ???

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Excel VBA: How To: Shell (Execute) thru a Shortcut ???

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel 2003

    Has anyone used a Windows "Shortcut" as an indirect address for a "Shell" command to execute a program with command line parameters?

    This would allow me to drag a shortcut to any users "C:\" root directory and use the shortcut to get to other resources, such as Adobe Distiller or whatever. In some cases that would be simpler than trying to dig the correct version of the correct executable out of the Registry, especially where there may be several versions of the program on the machine.

    Thank you for any and all comments, suggestions, and assistance with this inquiry.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA: How To: Shell (Execute) thru a Shortcut ???

    You can shellexecute the shortcut file which has an hidden .lnk extension even when the file has a extension.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_SHOWMINIMIZED As Long = 2
    8. Private Const SW_SHOWMAXIMIZED As Long = 3
    9.  
    10. Private Const ERROR_FILE_NOT_FOUND As Long = 2&
    11. Private Const ERROR_PATH_NOT_FOUND As Long = 3&
    12. Private Const ERROR_BAD_FORMAT As Long = 11&
    13. Private Const SE_ERR_ACCESSDENIED As Long = 5
    14. Private Const SE_ERR_ASSOCINCOMPLETE As Long = 27
    15. Private Const SE_ERR_DDEBUSY As Long = 30
    16. Private Const SE_ERR_DDEFAIL As Long = 29
    17. Private Const SE_ERR_DDETIMEOUT As Long = 28
    18. Private Const SE_ERR_DLLNOTFOUND As Long = 32
    19. Private Const SE_ERR_FNF As Long = 2
    20. Private Const SE_ERR_NOASSOC As Long = 31
    21. Private Const SE_ERR_OOM As Long = 8
    22. Private Const SE_ERR_PNF As Long = 3
    23. Private Const SE_ERR_SHARE As Long = 26
    24.  
    25. Private Sub Command1_Click()
    26.     On Error GoTo MyError
    27.     Dim lRet As Long
    28.     lRet = ShellExecute(0, "Open", "C:\Shortcut to Test.bat.lnk", vbNullString, "C:\", SW_SHOWNORMAL)
    29.     If lRet <= 32 Then
    30.         Select Case lRet
    31.             Case 0
    32.                 MsgBox "The operating system is out of memory or resources."
    33.             Case ERROR_FILE_NOT_FOUND
    34.                 MsgBox "The specified file was not found."
    35.             Case ERROR_PATH_NOT_FOUND
    36.                 MsgBox "The specified path was not found."
    37.             Case ERROR_BAD_FORMAT
    38.                 MsgBox "The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)."
    39.             Case SE_ERR_ACCESSDENIED
    40.                 MsgBox "The operating system denied access to the specified file."
    41.             Case SE_ERR_ASSOCINCOMPLETE
    42.                 MsgBox "The filename association is incomplete or invalid."
    43.             Case SE_ERR_DDEBUSY
    44.                 MsgBox "The DDE transaction could not be completed because other DDE transactions were being processed."
    45.             Case SE_ERR_DDEFAIL
    46.                 MsgBox "The DDE transaction failed."
    47.             Case SE_ERR_DDETIMEOUT
    48.                 MsgBox "The DDE transaction could not be completed because the request timed out."
    49.             Case SE_ERR_DLLNOTFOUND
    50.                 MsgBox "The specified dynamic-link library was not found."
    51.             Case SE_ERR_FNF
    52.                 MsgBox "The specified file was not found."
    53.             Case SE_ERR_NOASSOC
    54.                 MsgBox "There is no application associated with the given filename extension."
    55.             Case SE_ERR_OOM
    56.                 MsgBox "There was not enough memory to complete the operation."
    57.             Case SE_ERR_PNF
    58.                 MsgBox "The specified path was not found."
    59.             Case SE_ERR_SHARE
    60.                 MsgBox "A sharing violation occurred."
    61.         End Select
    62.     End If
    63.     Exit Sub
    64. MyError:
    65.     MsgBox Err.Number & " - " & Err.Description
    66. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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