wincc 7.5SP2下VBA程式設計學習練習15:批次刪除變數

来自金沙江的小鱼發表於2024-11-18

在前面練習的基礎上學習批次刪除變數。

新建下面的指令碼:

Sub DeleteTags()
'批次刪除變數
Dim hmigo As hmigo
Dim strTagName As String
Dim i As Integer
Set hmigo = New hmigo
For i = 1 To 5
strTagName = "Real" & CStr(i)
hmigo.DeleteTag strTagName
Next
Set hmigo = Nothing
End Sub

儲存,執行就會刪除Real1至Real5共計5個變數。如果這些變數已經被刪除了,執行指令碼就會報錯。

為了容錯性,對上面的指令碼重新編寫如下,先判斷要刪除的變數是否存在,存在才做刪除操作:

Sub DeleteTags()
'批次刪除變數
Dim hmigo As hmigo
Dim strTagName As String
Dim i As Integer
Set hmigo = New hmigo
'For i = 1 To 5
' strTagName = "Real" & CStr(i)
' hmigo.DeleteTag strTagName
'Next
For i = 1 To 5
strTagName = "Real" & CStr(i)
hmigo.ListTag TAG_NAMES, Tags, strTagName
If (UBound(Tags) - LBound(Tags) + 1) > 0 Then
hmigo.DeleteTag strTagName
End If
Next
Set hmigo = Nothing
End Sub

相關文章