Home
SEARCH

 

Requesting Data from WinWedge Using a VBA Subroutine


  Steps for setting up WinWedge

1. Select "DDE Server" from WinWedge "Mode" menu. When the dialog box appears asking for a DDE Command Destination Application, enter: "EXCEL" as the Application Name and then enter: "SYSTEM" as the DDE topic.

2. Select "Input Record Structure" in the "Define" menu and define the structure of the input record(s) to WinWedge. When you get to the final Window with the caption "Input Record Definition Editor", enter the string: [RUN("GetSWData")] as the Field Postamble DDE Command after the last data field that you have defined. This is a DDE command that will be sent to EXCEL after each data record is received by the Wedge.

3. Set up the rest of WinWedge parameters and then activate it.

  Steps for setting up MS Excel

1. Create or edit a macro module and enter the following code in the module:
(To create a new module click on Insert > Module from the Menus in the Visual Basic Editor Window.)

 

Sub GetSWData()
Dim R As Long
Dim X As Long
Dim Chan As Long
Dim NumFields As Long
Dim vDat As Variant
Dim sDat As String
 
' find the next empty row in Column A
R = Thisworkbook.Sheets("Sheet1").Cells(65000,1).end(xlup).row + 1

Chan = DDEInitiate("WinWedge", "Com1") ' Establish DDE link to WinWedge on Com1
' WinWedge can be configured to parse data into more than one data field. The following code demonstrates how to retrieve one or more data fields. If more than one field is specified, each field is placed in its own column.


' How many data fields are we retrieving from WinWedge?
NumFields = 1              

For X = 1 To NumFields  ' Loop through all data fields defined in the Wedge
    ' Request the data from each field in the wedge
    vDat = DDERequest(Chan, "Field(" & Cstr(X) & ")")
    ' Convert the data from a variant array data type to a string    
    sDat = vDat(1)       
    ' Place the data in cell location Row = R, Column = X

    Thisworkbook.Sheets("Sheet1").Cells(R, X).Value = sDat
Next

DDETerminate Chan ' Close the DDE channel

' Insert a date/time stamp in the sheet in the same row as the data
Thisworkbook.Sheets("Sheet1").Cells(R, NumFields + 1).
Value = Now

End Sub


The example above sets up WinWedge to issue a DDE command consisting of the Excel "RUN" command forcing Excel to run the subroutine "GetSWData()" after each data record is received from your serial device. The "GetSWData" subroutine performs a DDERequest to WinWedge that returns the contents of FIELD(1) to a variable named "WedgeData". The data is then assigned to a cell in "Sheet1" and a pointer variable is updated so that the next input will be written to the cell directly below the last input. The example above assumes that the open workbook contains a worksheet named Sheet1.

Back to Code Samples