Tick Talk on EsoxRepublic.com

“ShellExecute” to Open a File in Its Default App

Posted in API by Administrator on the September 26th, 2008

To introduce using Windows API in macros, I picked the simplest example I have in my toolbox: opening a file in its default application. Using the ShellExecute command, one can call a file to simply open in its default application, much the same way that double-clicking a file icon in an explorer window would do. I use it to add hyperlinks to my website in my published macros.

The “Declare” Statement

To use a Windows API function in a SolidWorks macro, one must first add the function to the macro code using a Declare statement. A declare statement acceesses and names a function in a Windows DLL library for use in VB code. Elements of a Declare statement are thus:

  • Name of function or sub
  • “Lib: name of DLL library containing function or sub
  • “Alias”: a user-defined alternate name for the function
  • function parameters
  • Return variable

An example of the declare statement for ShellExecute would look like this:

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

Using ShellExecute for Hyperlink

To make a hyperlink, I use ShellExecute to open a web page. The user’s operating system determines the default application with which to open the web page.

Public Sub OpenEsoxWeb()
ShellExecute 0, vbNullString, “http://www.EsoxRepublic.com”, vbNullString, “C:\”, SW_SHOWNORMAL
End Sub

Connect the OpenEsoxWeb sub to an object in a form to give that object a “hyperlink”.

Private Sub lblWebLink_Click()
End Sub

You can find an implementation of this in my Delete Custom Info macro.

Works with any file

Use ShellExecute with any file you want to open: web pages, MS Word, Excel, or PowerPoint Docs, images. Wherever you need the effect of a “double-click”.

AllAPI.net archive on Mentalis.org
The Borg

Thought for the day

The two best things to bring to a gunfight: a gun, and a friend with a gun.

Leave a Reply

You must be logged in to post a comment.