Execute Functions from VSTA (VB.NET)

Execute Functions from VSTA (VB.NET)

Im trying to run a function from VSTA and am getting an error that I dont know how to get around. Below is my code...
Public Sub Macro_Thickness_Screen()
'Initialize Machine Mode Variable
Dim MachineMode As String = Me.Variables.Item("sMachineMode").get_Value
'Initialize Function Variables
Dim ThicknessMainScreen As RtFunction
Dim ThicknessAutoScreen As RtFunction
Dim ThicknessManualScreen As RtFunction
Try
'Set Function Variables to the actual functions
--> ThicknessMainScreen = Me.RtFunctions.Item("scrPartThicknessMain")
ThicknessAutoScreen = Me.RtFunctions.Item("scrPartThicknessRightNormal")
ThicknessManualScreen = Me.RtFunctions.Item("scrPartThicknessRightManual")
Select Case MachineMode
Case "Automatic"
ThicknessMainScreen.Start()
ThicknessAutoScreen.Start()
Case ("Manual")
ThicknessMainScreen.Start()
ThicknessManualScreen.Start()
Case Else
'Machine Mode not selected
Dim ErrorMessage As String = "Error: Please select machine mode first"
Me.Variables.Item("Exception").set_Value(0, ErrorMessage)
End Select
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
The error is thrown when the code runs the following line...
ThicknessMainScreen = Me.RtFunctions.Item("scrPartThicknessMain")
It is an invalid cast error. Any ideas on what i need to do to make this work?
Thanks!

This is a migrated post! Originally posted on 08.09.2016 by user loucks80. Please be aware that information can be outdated.