By using expertatexcel.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

expertatexcel.com

 


RunProgram "http.." (File Associations need to set up)

The RunProgram user-defined subroutine is very important for VBA.

It allows you to:
  • Open websites
  • Open data files (txt, log, docx, pdf)
  • open programs (excel, word, file explorer)
  • Visit webpage so you don't lose access

    It comes in handy.

    To set it up you first need a declaration and uses something called the Windows API.

    It is included in the ExperAtExcel.bas file that comes with this book.

    Here are some videos to watch this command in action.



    To call the routine

    RunProgram "https//google.com"

    The code is below:

    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

    The declaration above may need the following depending on the version of software you use
    #if Win64
       Public Declare PtrSafe 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
    #Else
       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
    #end if

    Sub RunProgram(sFile, Optional args, Optional runInFolder)
    'Version: 1.000
    'Purpose: This run passed sFile
      Dim RetVal As Long
      On Error Resume Next
      RetVal = ShellExecute(0, "open", sFile, "", "", 1)
    End Sub


    More Examples
  • to open a file in a cell - RunProgram Activecell
  • to Go to a website - RunProgram "https://Cherper.com"
  • to Query a cell in Google - RunProgram "https://www.google.com/search?q=" & ActiveCell

    Exercise
    1. Create a shortcut key Ctrl G to Google what's in a cell
    2. Using Ctrl R to randomly search on Google or Bing or DuckDuckGo or Amazon
    3. Using Ctrl T Post something to Twitter


    Solutions:

  •