1. 程式人生 > >VBA操作登錄檔的三種方法

VBA操作登錄檔的三種方法

1. 利用VBA內建的方法實現。VBA內建操作登錄檔的四個函式分別是SaveSetting(寫入登錄檔)、GetSetting(讀取登錄檔)、GetAllSettings(獲得登錄檔的鍵和值)和DeleteSetting(刪除鍵或值)。但內建方法只能操作登錄檔中的HKEY_CURRENT_USER\Software\VB and VBAprogramSettings下的子鍵或者登錄檔

Sub Build_inReg()

Dim xRegKey As Variant

Dim xRegValue As Variant

Dim i As Integer

Dim xValue As String

Dim xAllKeyValueArr As Variant

On Error Resume Next

xRegKey = Array("KeyOne", "KeyTwo","KeyThree")

xRegValue = Array("ValueOne", "ValueTwo","ValueThree")

'寫入登錄檔

For i = 0 To UBound(xRegKey)

  SaveSetting"ProjectName", "RegItems", xRegKey(i), xRegValue(i)

Next

'讀取登錄檔

For i = 0 To UBound(xRegKey)

  xValue =GetSetting("ProjectName", "RegItems", xRegKey(i),xRegValue(i))

  MsgBox xValue

Next

'獲取所有鍵值

xAllKeyValueArr = GetAllSettings("ProjectName","RegItems")

'刪除鍵值

DeleteSetting "ProjectName","RegItems"

End Sub

2. 利用WindowsScripting Host的登錄檔函式來完成。WSH的RegWrite(設定登錄檔的鍵和值),RegRead(獲得登錄檔的鍵和值),RegDelete(刪除鍵和值)。

Sub WSHReg()

Dim xRegMainKey As String

Dim xRegKey As Variant

Dim xRegValue As Variant

Dim i As Integer

Dim xValue As String

Dim xWSH As Object

On Error Resume Next

Set xWSH = CreateObject("WScript.Shell")

xRegMainKey = "HKEY_CURRENT_USER\Software\" '按自己需求選擇登錄檔主鍵

xRegKey = Array("KeyOne", "KeyTwo","KeyThree", "KeyFour")

xRegValue = Array("ValueOne", "Valuetwo","ValueThree", "ValueFour")

'寫入登錄檔

For i = 0 To UBound(xRegKey)

  xWSH.RegWritexRegMainKey + "ProjectName" + "\" + "RegItems" +"\" + xRegKey(i), xRegValue(i) '(xRegMainKey +"ProjectName" + "\" + "RegItems" + "\"+ xRegKey(i) + xRegValue(i))

Next

'讀取登錄檔

For i = 0 To UBound(xRegKey)

  xValue =xWSH.RegRead(xRegMainKey + "ProjectName" + "\RegItems\" +xRegKey(i))

  MsgBox xValue

Next

'刪除登錄檔

xWSH.RegDelete xRegMainKey & "ProjectName"& "\RegItems\"

xWSH.RegDelete xRegMainKey & "ProjectName\"

End Sub

3.使用WMI(WindowsManagement Instrumentation)物件的StdRenProv類操作登錄檔,沒有範圍限制、功能強大。

Sub WMIReg()

Dim xValue

Dim xName

Dim xType

Dim i As Integer

Dim xStrTemp As String

Dim xWMIObj As Object

On Error Resume Next

Const HKEY_CURRENT_USER = &H80000001

Set xWMIObj = GetObject("winmgmts:\\.\root\default:StdRegProv")

xWMIObj.CreateKey HKEY_CURRENT_USER,"MyTest\test"                 '建立登錄檔鍵值

'寫入登錄檔

xWMIObj.SetBinaryValue HKEY_CURRENT_USER, "MyTest\test","test1", Array(&H0, &H0, &H1)

xWMIObj.SetStringValue HKEY_CURRENT_USER,"MyTest\test", "test2", "2"

xWMIObj.SetDWORDValue HKEY_CURRENT_USER,"MyTest\test", "test3", "3"

'讀取登錄檔

xWMIObj.getbinaryvalue HKEY_CURRENT_USER,"MyTest\test", "test1", xValue

For i = 0 To UBound(xValue)

  MsgBox xValue(i)

Next

'xWMIObj.deletevalue HKEY_CURRENT_USER,"MyTest\test", "test1"

xWMIObj.EnumValues HKEY_CURRENT_USER,"MyTest\test", xName, xType

For i = 0 To UBound(xName)

  If xType(i) = 1Then

   xWMIObj.Getstringvalue HKEY_CURRENT_USER, "MyTest\test",xName(i), xValue

    MsgBox xValue

  ElseIf xType(i) =4 Then

   xWMIObj.GetDWORDValue HKEY_CURRENT_USER, "MyTest\test",xName(i), xValue

    MsgBox xValue

  End If

Next

'刪除登錄檔

xWMIObj.DeleteKey HKEY_CURRENT_USER,"MyTest\test"

xWMIObj.DeleteKey HKEY_CURRENT_USER, "MyTest"

End Sub

注意:

1:

HKEY_CLASSES_ROOT       0x80000000

HKEY_CURRENT_USER       0x80000001

HKEY_LOCAL_MACHINE       0x80000002

HKEY_USERS       0x80000003

HKEY_CURRENT_CONFIG       0x80000005

2:

REG_SZ(字串型)       1

REG_EXPAND(可擴充字串型)       2

REG_BINARY(二進位制型)       3

REG_DWORD(雙位元組型)       4

REG_MULTI_SZ(多字串型)       7