Software Support

Launching WinWedge from an Excel macro

The following VBA subroutine executes WinWedge and passes it the name of a configuration file (MyConfig.SW3) on the command line. This causes WinWedge to automatically load the configuration file and activate itself in NORMAL mode.

Launching WinWedge from Excel Example #1
Public Const MyPort$ = "COM1" ' change port if necessary

Sub RunWedge() 
   On Error Goto ErrorHandler 'trap errors
       ' try to activate the Wedge Window
   ' if Wedge is not activated, this will generate an error
   AppActivate "Software Wedge - " & MyPort$

   ''''''''''''''''''''''''''''''''''''''''''''''
   ' NOTE: For WinWedge 3.0 Standard Edition use:
   ' AppActivate "WinWedge - " & MyPort$
   '''''''''''''''''''''''''''''''''''''''''''''' 
   On Error Goto 0' remove error trap
   ' Set the focus back to excel & exit. Comment out if running Excel 2010
   AppActivate Application.Caption
Exit Sub

ErrorHandler:

   ' If we get here the wedge is not running, so launch it:
   RetVal = Shell("C:\WINWEDGE\WINWEDGE.EXE _
   C:\WINWEDGE\MyConfig.SW3")

   ' give wedge time to load ~ 2 secs, then resume.
   Application.Wait Now + TimeValue("00:00:02")
   Resume Next
End Sub
Launching WinWedge from Excel Example #2

The following subroutine does the same thing as the code above except that it launches WinWedge configuration file directly using the Windows API function "ShellExecute". The advantage of the following method is that you do not need to know the location of WinWedge.EXE program file and you only need to know the file location for the WinWedge configuration file that you want to use.

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

Sub LaunchWinWedgeConfig()
   Dim WedgeFile As String, X As Long
   WedgeFile = "MyConfig.SW3" 'WinWedge config file name
   ' assume the config file is in the same file folder as this
   ' workbook - if not, code the full path for the file into
   ' the WedgeFile variable and delete the following line
   WedgeFile = ThisWorkbook.Path & "\" & WedgeFile
   ' launch the WinWedge configuration file
   X = ShellExecute(0, "open", WedgeFile, vbNullString, vbNullString, 0)
   If X < 33 Then ' function failed - warn user and exit
      MsgBox "The file: " & WedgeFile & " did not launch successfully"
   Else ' otherwise, wait 3 secs for WinWedge to load 
      Application.Wait Now + TimeValue("00:00:03")
      AppActivate Application.Caption ' set focus to Excel
   End If
End Sub
Launching WinWedge from Excel Example #3

This is a very straightfoward method of opening up a file through Excel. Depending on your usage, this may be more flexible or less flexible than the previous examples.

Sub Auto_Open()
    Dim x As Variant
    Dim Path As String
    Dim File As String

    ' Set the Path variable equal to the path of your WinWedge installation
    Path = "C:\Program Files\WinWedge\WinWedge.exe"

    ' Set the File variable equal to the path of your saved configuration file
    File = "C:\Program Files\WinWedge\YOURFILENAME.SW3"

    x = Shell(Path + " " + File, vbNormalFocus)
End Sub

Note: If you name an Excel subroutine "Auto_Open()", Excel will automatically run the routine whenever the spreadsheet that contains it is initially opened. The Auto_Open subroutine is an excellent place to launch the Wedge (as well as do any other initialization functions that you might require). If you re-name the above subroutine to "Auto_Open()" then you can save yourself the step of having to manually run the above subroutine.

Similar to the Auto_Open subroutine, Excel also supports an Auto_Close subroutine that runs automatically when you close your spreadsheet. This might be a good place to tell the Wedge to quit and unload itself from memory as in the following example.

' this sub runs when you close the sheet
Sub Auto_Close()
   On Error Resume Next ' ignore errors

   ' open a dde link with the wedge
   chan = DDEInitiate("WinWedge", MyPort$)
   DDEExecute chan, "[Appexit]" ' tell wedge to quit 
   DDETerminate chan 'Terminate the channel
End Sub

 

Categories: Macro / Code Sample, Microsoft Excel, WinWedge
Last Updated: 2011.09.15
Need more help?

Don't hesitate to call or email us with your questions!

Technical Support: 215-496-0222

Toll-Free: 1 (800) 722-6004
Skype: taltech1
Email: support@TALtech.com
Our office is open 9AM - 5PM Monday Through Friday (E.S.T.)