Thursday, December 22, 2016

Reg Del( search for specific string in key)

Const HKCU = &H80000001

sStartKeyPath =  "Software\Microsoft\Office\15.0\Lync\"
sKey = "xyz.com.au"

Set oReg = GetObject _
     ("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

SearchAndDeleteRegistryKey HKCU, sStartKeyPath, sKey


Sub SearchAndDeleteRegistryKey(ByVal sHive, ByVal sStartKeyPath, ByVal sKey)
  Dim aSubKeys, sSubKey, iRC
  On Error Resume Next
  iRC = oReg.EnumKey(sHive, sStartKeyPath, aSubKeys)
  If iRC = 0 And IsArray(aSubKeys) Then
    For Each sSubKey In aSubKeys
      If Err.Number <> 0 Then
        Err.Clear
        Exit Sub
      End If
      SearchAndDeleteRegistryKey sHive, sStartKeyPath & "\" & sSubKey, sKey
    Next
  End If

   aTmpPath = Split(sStartKeyPath, "\")
   If InStr(1, aTmpPath(UBound(aTmpPath)), sKey, vbTextCompare) > 0 Then

    DeleteRegistryKey sHive, sStartKeyPath
  End If
End Sub


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
  Dim aSubKeys, sSubKey, iRC
  On Error Resume Next
  iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
  If iRC = 0 And IsArray(aSubKeys) Then
    For Each sSubKey In aSubKeys
      If Err.Number <> 0 Then
        Err.Clear
        Exit Sub
      End If
      DeleteRegistryKey sHive, sKey & "\" & sSubKey
    Next
  End If
  oReg.DeleteKey sHive, sKey
End Sub


No comments:

Post a Comment