Nếu bạn không muốn sử dụng các hàm API, bạn có thể sử dụng thư viện đối tượng Words để đọc và ghi Chuỗi hồ sơ cá nhân.

Words System.PrivateProfileString có thể đọc và ghi vào cả INI-files và Registry.

Trong các ứng dụng khác ngoài Word, bạn phải thêm một tham chiếu đến thư viện đối tượng Words.

Bạn có thể thêm tham chiếu bằng cách mở Trình soạn thảo Visual Basic (VBE) và kích hoạt Dự án VB của bạn. Sau đó, bạn chọn Tools, References …​ và đánh dấu vào tùy chọn Microsoft Word x.x Object Library.

Ghi thông tin vào INI-files

Với macro dưới đây, bạn có thể lưu thông tin trong một tệp văn bản:

Function SetIniSetting(FileName As String, Section As String, _

Key As String, KeyValue) As Boolean

Dim wd As Word.Application

SetIniSetting = False

Set wd = New Word.Application ' create the Word application object

On Error Resume Next

wd.System.PrivateProfileString(FileName, Section, Key) = CStr(KeyValue)

On Error GoTo 0

Set wd = Nothing ' destroy the Word application object

SetIniSetting = True

End Function

Sử dụng macro như thế này để lưu giá trị 100 trong tệp C: \ FolderName \ FileName.ini trong phần MySectionName cho khóa TestValue:

MyBooleanVar = SetIniSetting (“C: \ FolderName \ FileName.ini”, “MySectionName”, “TestValue”, 100)

Tệp văn bản sẽ có dạng như sau:

TestValue = 100

Đọc thông tin từ INI-files

Với macro bên dưới, bạn có thể đọc thông tin từ tệp văn bản:

Function GetIniSetting(FileName As String, Section As String, _

Key As String) As String

Dim wd As Word.Application

GetIniSetting = ""

Set wd = New Word.Application ' create the Word application object

On Error Resume Next

GetIniSetting = wd.System.PrivateProfileString(FileName, Section, Key)

On Error GoTo 0

Set wd = Nothing ' destroy the Word application object

End Function

Sử dụng macro như thế này để trả về giá trị cho khóa TestValue trong phần MySectionName từ tệp C: \ FolderName \ FileName.ini:

MyStringVar = GetIniSetting("C:\FolderName\FileName.ini", _

"MySectionName", "TestValue")

===

Ghi thông tin vào Registry * Với macro bên dưới, bạn có thể lưu thông tin vào Registry:

Function SetRegistrySetting(Section As String, _

Key As String, KeyValue) As Boolean

Dim wd As Word.Application

SetRegistrySetting = False

Set wd = New Word.Application ' create the Word application object

On Error Resume Next

wd.System.PrivateProfileString("", Section, Key) = CStr(KeyValue)

On Error GoTo 0

Set wd = Nothing ' destroy the Word application object

SetRegistrySetting = True

End Function

Sử dụng macro như thế này để lưu một giá trị mới trong HKEY_CURRENT_USER \ Software \ Microsoft \ Office8.0 \ Excel \ Microsoft Excel cho khóa DefaultPath:

MyStringVar = "HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Microsoft Excel"

MyBooleanVar = SetRegistrySetting(MyStringVar, _

"DefaultPath", "C:\FolderName")

Đọc thông tin từ Registry Với macro bên dưới, bạn có thể đọc thông tin từ Registry:

Function GetRegistrySetting(Section As String, Key As String) As String

Dim wd As Word.Application

GetRegistrySetting = ""

Set wd = New Word.Application ' create the Word application object

On Error Resume Next

GetRegistrySetting = wd.System.PrivateProfileString("", Section, Key)

On Error GoTo 0

Set wd = Nothing ' destroy the Word application object

End Function

Sử dụng macro như thế này để đọc giá trị từ khóa DefaultPath

từ HKEY_CURRENT_USER \ Software \ Microsoft \ Office8.0 \ Excel \ Microsoft Excel:

MyStringVar = "HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Microsoft Excel"

MyStringVar = SetRegistrySetting(MyStringVar, _

"DefaultPath")