Jump to content

Excel VBA Load UniDDE Software and Run Project


Recommended Posts

Posted

In UniDDE, if you right click on your open project or use the menu buttons, you can copy the Read Command, Stop PLC Macro, Run PLC Macro, and Run/Stop Toggle macro.  If I have the read command in an Excel worksheet, it tries to start UniDDE.exe but fails.  Instead, I had to clear the field until the workbook is open then add the read command =UniDDE|Items!'lblDDE(1)' to the cell afterwards. 

My second issue (the first being that I can't seem to get the UniDDE read command to automatically load the software) is that the PLC is not in Run mode.  Therefore, I try to use the code that UniDDE provides with those copy buttons.  If I use the "Run/Stop" toggle it will properly stop the PLC connection if it's already running.  If I execute that code to make it run, it will run but Excel freezes until I click escape.  I also tried the "Run PLC" macro, but it does absolutely nothing.

 

My VBA to start the UniDDE software is below. 

Cell F1 = "C:\Program Files (x86)\Unitronics\Unitronics DDE Server\UniDDE.exe"

Cell F2 = "UniDDE.exe"

Cell F3 = "U:\Display Result and Write CSV.ude"

Sub Load_UniDDE()
    Application.StatusBar = "loading PLC application"
    
    Dim strApp, strAppShort, strFile As String
    Dim WB As Workbook
    Set WB = ThisWorkbook
    strApp = WB.Worksheets(1).Range("F1").Value
    strAppShort = WB.Worksheets(1).Range("F2").Value
    strFile = WB.Worksheets(1).Range("F3").Value
        

    If strApp = "" Or strAppShort = "" Then
        Application.WindowState = xlMaximized
        WB.Worksheets(1).Range("F1").Select
        
        MsgBox "Please correct the software path and process in the Excel worksheet."
    Else
        If Dir(strFile) <> "" Then
            Dim objList As Object
            
            Set objList = GetObject("winmgmts:").ExecQuery("select * from win32_process where name='" & strAppShort & "'") ' determine if the DDE app is already running
            If objList.Count = 0 Then
                Shell strApp & " " & strFile, vbMinimizedFocus ' open the DDE app and project file
            End If

        Else
            Application.WindowState = xlMaximized
            WB.Worksheets(1).Range("F3").Select
            MsgBox "Please correct the project path in the Excel worksheet."
        End If
    End If
        
    Application.StatusBar = False
End Sub

UniDDE provides the following macros which are not working properly

RUN PLC:

Dim lchannelNumber   As Long

   '/ Run PLC 
   lchannelNumber = Application.DDEInitiate("UniDDE", "Items")
   Application.DDEExecute lchannelNumber, "Last Test Result_RUN"
   Application.DDETerminate lchannelNumber

RUN/STOP Toggle:

Dim lchannelNumber   As Long

   '/ Run/stop UniDDE 
   lchannelNumber = Application.DDEInitiate("UniDDE", "Items")
   Application.DDEExecute lchannelNumber, "UniDDE_RUN/STOP"
   Application.DDETerminate lchannelNumber

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.