ExcelTip.com
ExcelTip.com
Account Icon Account Icon Account Icon
Google Exceltip.com
JOIN OUR NEWSLETTER
  and receive for joining:
Free eBook Learn More!
Free Excel ADD-IN
Free Weekly Excel Tip
4 e-books in cd-rom
F1 Formulas & Functions
F1 Formulas & Functions
F1 EXCEL
F1 eBook (Spanish)
F1 EXCEL
Financial Statements.xls
 

» Private Profile Strings using Words System.PrivateProfileString using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
If you don't want to use API-functions you can use Words object library to read and write Private Profile Strings.

Words System.PrivateProfileString can read from and write to both INI-files and the Registry.

In other applications than Word you have to add a reference to Words object library.

You can add the reference by opening the Visual Basic Editor (VBE) and activate your VB Project. Then you select Tools, References... and check the option Microsoft Word x.x Object Library.

Write information to INI-files

With the macro below you can save information in a text file:
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
Use the macro like this to save the value 100 in the file C:\FolderName\FileName.ini in the section
MySectionName for the key TestValue:
MyBooleanVar = SetIniSetting("C:\FolderName\FileName.ini", "MySectionName", "TestValue", 100)
The text file will look like this:
[MySectionName]
TestValue=100


Read information from INI-files
With the macro below you can read information from a text file:
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
Use the macro like this to return the value for the key TestValue in the section MySectionName
from the file C:\FolderName\FileName.ini:
MyStringVar = GetIniSetting("C:\FolderName\FileName.ini", _
    "MySectionName", "TestValue")

Write information to the Registry


With the macro below you can save information in the 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
Use the macro like this to save a new value in HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\Microsoft
Excel for the key DefaultPath:
MyStringVar = "HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\Microsoft Excel"
MyBooleanVar = SetRegistrySetting(MyStringVar, _
    "DefaultPath", "C:\FolderName")
Read information from the Registry With the macro below you can read information from the 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
Use the macro like this to read the value from the key DefaultPath
from HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\Microsoft Excel:
MyStringVar = "HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\Microsoft Excel"
MyStringVar = SetRegistrySetting(MyStringVar, _
    "DefaultPath")


Rate this tip
12 34 5
  RATING: 4.75
  VIEWS: 15945

READER COMMENTS (view all comments)


No comments have been submitted.


REGISTERED USERS - Click here to post comments


GUESTS (Click here to register)
Name
Comment Title
Comments

The One Page Business Plan: Start With a Vision, Build a Company!

Investing in Real Estate, Fourth Edition

Seven Habits Of Highly Effective People

Finance and Accounting for Nonfinancial Managers

Essentials of Investments with Standard & Poor's Educational Version of Market Insight + PowerWeb + Stock Trak Coupon

Rich Dad, Poor Dad: What the Rich Teach Their Kids About Money--That the Poor and Middle Class Do Not!

RELATED MICROSOFT EXCEL TIPS


Convert PDF Files to Excel






Excel VBA books
Accounting books
Business Plans
MS Office books
Taxes books

VIEW ALL BOOKS


  Advertise With Us                               

Tips

Add-In in VBA | Applications - Word, Outlook in VBA | Array Formulas | Cells, Ranges, Rows, and Columns in VBA | Counting | Custom Functions | Custom Functions in VBA | Database Formulas | Database in VBA | Date & Time Formulas | Date & Time in VBA | Events in VBA | Excel 2003 | Excel Chart | Excel Consolidating | Excel Counting | Excel Custom Functions using VBA | Excel Customizing | Excel Data | Excel Dates | Excel Editing | Excel Files | Excel Filter | Excel Format | Excel Formula | Excel General | Excel Grouping and Outlining | Excel Importing Text Files | Excel Information | Excel Keyboard Shortcuts | Excel Loan Formulas | Excel Macros - VBA | Excel Pivot Tables | Excel Printing | Excel Range Name | Excel Security - Protection | Excel Sorting | Excel Style | Excel Subtotals | Excel Summing | Excel Text | Excel Time | Excel Tools | Excel Worksheet, Workbook | Files, Workbook, and Worksheets in VBA | Financial Formulas | Formating in VBA | General Topics in VBA | Import and Export in VBA | Information Formulas | Keyboard & Other Shortcuts in VBA | Keyboard Formula Shortcuts | Links between Worksheet and Workbooks | Links in VBA | Logical Formulas | Lookup Formulas | Mail - Send and Receive in VBA | Menus, Toolbars, Status bar in VBA | Modules, Class Modules in VBA | Other Q&A Formulas | Printing in VBA | Protecting in VBA | Summing | Text Formulas | User Forms, Input boxes in VBA | Using Loops | Working with Formulas |

Tips by Version

Microsoft Excel 97 | Microsoft Excel 2000 | Microsoft Excel 2002 | All Microsoft Excel Versions | New in Excel 2002 | New in Excel 2003 - Office 11

Website

Home | Tip of Hour | Recommended Tips | Most Viewed Tips | Tips by Version | Submit a Tip | My Tips
Microsoft Excel Tutorials | Excel Links | Write for Us | About Us | Search Results | Tip Archives | Excel Forum | Excel Forum Archives

Excel Book

Excel 97 Book | Excel 2000 Book | Excel 2002 Book | Excel XP Book | Book Store

Terms and Conditions of use
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Copyright © 2003 ExcelTip.com
Microsoft, Microsoft Excel is a U.S. registered trademark of Microsoft Corporation
Site Developed By: Varien