2013-05-02 103 views
1

我是新來編程,所以我希望你能幫助我。不知道如何制定正確的問題,所以我沒有找到任何StackOverflow答案。VBA子程序不退出循環

當我單步執行此代碼並訪問OpcServObj.Connect OpcServerStrg時,它無法連接。我沒有任何錯誤,沒有任何事情發生。光標跳到左邊距並且什麼都不做。只有當我重新開始執行程序時,纔會再次啓動該功能。它應該繼續並退出for循環,我打算再次測試連接的狀態。

參考庫: OPC DA自動化包裝2.02

Sub Main() 
ConnectOPC 
End Sub 

Private Function ConnectOPC() As Boolean 

Dim OpcServObj As OPCAutomation.OPCServer    
Dim OpcGroupObj As OPCAutomation.OPCGroup    

Dim OPCServerlist As Variant 
Dim OpcServerStrg As String 

Set OpcServObj = New OPCAutomation.OPCServer   
OPCServerlist = OpcServObj.GetOPCServers 

If OpcServObj.ServerState <> True Then 

    For i = 1 To UBound(OPCServerlist) 

     OpcServerStrg = OPCServerlist(i) 

     If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then 

      OpcServObj.Connect OpcServerStrg 

      Exit For 

     End If 

    Next i 

End If 

End Function 

由於

回答

0

在生產線:

If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then 

如果在可變OpcServerStrg等於到"ICONICS.IconicsOPCUAServer.V5"然後代碼將再次循環到For循環的頂部,並再次執行OpcServerStrg = OPCServerlist(i)。將不會看到Exit For

當你說你step through this code ...是有可能,你有一個斷點上線設置:OpcServerStrg = OPCServerlist(i),和你點擊運行(直到斷點),而不是通過代碼點擊單步?

嘗試在該行設置斷點:

If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then 

然後檢查的OpcServerStrg值當程序到達斷點並停止。

+0

OpcServerStrg的值得到滿足。這是當我執行OpcServObj.Connect(OpcServerStrg)我有問題。 – BgreenDSI 2013-05-02 17:01:00

+0

斷點處的值爲「ICONICS.IconicsOPCUAServer.V5」 – BgreenDSI 2013-05-02 17:03:41

+0

我在出口處放置了一個斷點,然後繼續逐步執行程序。謝謝。 – BgreenDSI 2013-05-02 17:09:37