“ShellExecute” to Open a File in Its Default App
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()
OpenEsoxWeb
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”.
Links:
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.
SW BOM to Excel Macro
…not quite, but a fast way from here to there.
I just released a macro I have been using for a few months. “Copy BOM to Clipboard” macro does exactly what it says: it copies a table-style BOM from a SW drawing to the clipboard in a format that can readily be pasted into MS Excel. Download here.
Just run the macro with a SW drawing open. If successful, there will be a message like that shown below. Then go to an Excel worksheet, select a cell, and paste (”ctrl-v”).
| Message box indicating successful acquisition of BOM. |
Developer’s notes
It’s been a while, but I am trying to remember some of the specific challenges of writing this. It took about two hours of fumbling until the error-to-trial ratio dropped below 1.0.
Application writing is often mostly about validation. Checking to see if there is an active document, is it a drawing, does it have a BOM (or more than one). That took most of the time.
There was a hook to getting ahold of the table data. I put the BOM data first into a SldWorks.BomTableAnnotation object, then moved it to a SldWorks.TableAnnotation object which could then be parsed into an array. The final array is then written to a tab-delimited string which pastes so nicely into Excel.
It could have been more elegant, but it “works fine, lasts long time”.
Thought for the day
Speak softly and carry a big stick. Some folks out there can take a punch much better than they can take an insult.