Use either of the two examples below to launch WinWedge directly from Excel. The first example uses the ShellExecute function as defined. This allows you to launch the WinWedge configuration file just knowing the file name and path. If it is located in the same folder as the Excel workbook, then you can just use the file name. The second example uses the Shell function and requires that you know the full path of WinWedge and the file name and/or path of the configuration file.
Launching WinWedge from Excel using ShellExecute (Recommended)
This example just requires that you know the file name of the WinWedge configuration file, or if the file is stored in a different location than the Excel workbook, the full path of the file. This code is compatible with both 32-bit and 64-bit versions of Office.
#If VBA7 Then
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
#Else
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 Auto_Open()
OpenFile "device.SW3" ' or C:\FullPath\device.SW3
End Sub
Sub OpenFile(ByVal File As String)
Dim FullPath As String
#If VBA7 Then
Dim x As LongPtr
#Else
Dim x As Long
#End If
'Determine the full path of the file
If InStr(File, "\") = 0 Then
FullPath = ThisWorkbook.Path & "\" & File
Else
FullPath = File
End If
'Launch the configuration file
x = ShellExecute(0, "open", FullPath, vbNullString, vbNullString, 0)
'Prompt the user if launch fails
If x < 33 Then
MsgBox "The file: " & FullPath & " did not launch successfully"
End If
End Sub
Launching WinWedge from Excel using Shell
This example requires you to put in the full path of the file and the full path of the WinWedge executable. Use this if you encounter any issues with the code above or if you want to avoid using ShellExecute.
Sub Auto_Open()
OpenFileAlt "device.SW3", "C:\FullPath\WinWedge.exe"
End Sub
Sub OpenFileAlt(ByVal File As String, ByVal WinWedge As String)
Dim FullPath As String, x As Long
'Determine the full path of the file
If InStr(File, "\") = 0 Then
FullPath = ThisWorkbook.Path & "\" & File
Else
FullPath = File
End If
'Launch the configuration file
x = Shell(WinWedge + " " + FullPath, vbNormalFocus)
'Prompt the user if launch fails
If x < 33 Then
MsgBox "The file: " & FullPath & " did not launch successfully"
End If
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).
Close WinWedge from Excel
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 code automatically runs when Excel is closed
Sub Auto_Close()
'Ignore Errors
On Error Resume Next
' Open a DDE link with WinWedge
chan = DDEInitiate("WinWedge", "COM1")
' Tell WinWedge to Quit
DDEExecute chan, "[Appexit]"
' Terminate the DDE link
DDETerminate chan
End Sub