Private Sub CommandButton1_Click()
Call TestRegistry
End Sub
Sub TestRegistry()
Dim myRegKey As String
Dim myValue As String
Dim myAnswer As Integer
'input chiave di registro da modificare
myRegKey = InputBox("Inserisci percorso chiave", _
"Get Registry Key")
x = msgbox("Il percorso è""" & _
myRegKey & """" & vbCr, vbOK)
If x = vbOK Then
If myRegKey = "" Then Exit Sub
'check se esiste
If RegKeyExists(myRegKey) = True Then
'se esiste la legge
myValue = RegKeyRead(myRegKey)
'mostra il valore e chiede se lo si vuole cambiare
myAnswer = msgbox("Il valore per la chiave """ & _
myRegKey & """" & vbCr & " è """ & myValue & _
"""" & vbCr & vbCr & _
"Vuoi cambiarlo?", vbYesNo)
Else
'se non viene trovata da errore
myAnswer = msgbox("La chiave nel percorso """ & myRegKey & _
""" non è stata trovata.")
End If
If myAnswer = vbYes Then
'ask for new registry key value
myValue = InputBox("Inserisci il nuovo valore:", _
myRegKey, myValue)
If myValue <> "" Then
'save/create registry key with new value
RegKeySave myRegKey, myValue
msgbox "Valore modificato."
End If
End If
Else: Exit Sub
End If
End Sub
'legge il valore della chiave
'se la chiave non viene trovata il valore è ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object
On Error Resume Next
'accesso a Windows scripting
Set myWS = CreateObject("WScript.Shell")
'legge la chiave dal registro
RegKeyRead = myWS.RegRead(i_RegKey)
End Function
'restituisce True se la chiave di registro i_RegKey è stata trovata
'sennò falso False
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object
On Error GoTo ErrorHandler
'accesso Windows scripting
Set myWS = CreateObject("WScript.Shell")
'prova a leggere la chiave di registro
myWS.RegRead i_RegKey
'se chiave trovata
RegKeyExists = True
Exit Function
ErrorHandler:
'se chaive non trovata
RegKeyExists = False
End Function
'imposta la chiave di registro i_RegKey
'al valore i_Value con il tipo i_Type
'se i_Type non è specificato, il tipo string
'se i_RegKey non è stata trovata, una nuova chiave di registro viene creata
Sub RegKeySave(i_RegKey As String, _
i_Value As String, _
Optional i_Type As String = "REG_DWORD")
Dim myWS As Object
'accesso a Windows scripting
Set myWS = CreateObject("WScript.Shell")
'scrittura della chiave di registro
myWS.RegWrite i_RegKey, i_Value, i_Type
End Sub
|