Microsoft KB Archive/81793

{|
 * width="100%"|

WD: Macros to Read Data from a Private .ini File

 * }

Q81793

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1, 6.0.1a

-

SUMMARY
You can write macros that use a settings file to store and retrieve settings. For example, you can store the name of the active document when you quit Word so that it can be reopened automatically the next time you start Word. In Windows, a settings file is a text file with information arranged like the information in the Windows 3.x Win.ini file. On the Macintosh, a settings file is a resource file such as Word Settings (97).

This article discusses methods you can use to extract information from a settings file other than the Win.ini file. Each method and example is specific to a version of Microsoft Word.

Word for Windows 6.0, 7.0, 7.0a
WARNING: ANY USE BY YOU OF THE MACRO CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

GetPrivateProfileString:

The GetPrivateProfileString function was added in Word version 6.0. Use GetPrivateProfileString to return a setting from a private settings file.

A settings file is a text file such as Win.ini that your macros can use to store and retrieve settings.

The following is an example of the GetPrivateProfileString$ command.

  Sub MAIN temp$ = GetPrivateProfileString$(&quot;Section Name&quot;, &quot;keyname&quot;, \      &quot;H:\winword\text.ini&quot;) MsgBox temp$ End Sub The above macro reads the contents the Text.ini file and posts the contents of &quot;keyname=&quot; heading in a message box.

The GetPrivateProfileString function reads a character string from the specified section of the specified initialization file. The return value is the number of characters copied into the buffer if successful or 0 (zero) if unsuccessful.

Word for the Macintosh 6.0, 6.0.1
The following is an example of the GetPrivateProfileString$ command.

  Sub MAIN temp$ = GetPrivateProfileString$(&quot;Microsoft Word&quot;, &quot;INI-Path&quot;, \      &quot;Word Settings (6)&quot;) MsgBox temp$ End Sub The above macro reads the contents the Word Settings (6) file and posts the contents of &quot;INI-Path=&quot; heading in a message box.

The GetPrivateProfileString function reads a character string from the specified section of the specified initialization file. The return value is the number of characters copied into the buffer if successful or 0 (zero) if unsuccessful.

Word for Windows 2.x
According to the Microsoft Windows Software Development Kit documentation, the GetPrivateProfileString function has the following parameters:

  INT GetPrivateProfileString(lpszSectionName, lpszKeyName, lpszDefault,   lpszReturnBuffer, cbReturnBuffer, lpszFileName)   LPCSTR lpszSectionName;    section name LPCSTR lpszKeyName;       keyname LPCSTR lpszDefault;       default return string LPSTR lpszReturnBuffer;   variable assignment for INI information int cbReturnBuffer;       size of the destination buffer LPCSTR lpszFileName;      initialization filename

[section name] keyname=string to read The following WordBasic macro demonstrates the use of the Windows GetPrivateProfileString function:

  Declare Function GetPrivateProfileString Lib &quot;kernel&quot; \ (lpApplicationName$, lpKeyName$, lpDefault$, lpReturnedString$, \   nSize As Integer, lpFileName$)  As Integer

Sub MAIN a = GetPrivateProfileString(&quot;Section Name&quot;, &quot;keyname&quot;, &quot;&quot;, Temp$,\      255,&quot;H:\WINWORD\Text.ini&quot;) MsgBox Temp$ End Sub The above macro reads the contents the Text.ini file and posts the contents of &quot;keyname=&quot; heading in a message box.

The GetPrivateProfileString function reads a character string from the specified section of the specified initialization file. The return value is the number of characters copied into the buffer if successful or 0 (zero) if unsuccessful.

NOTE: To obtain a string from the Win.ini file use the WordBasic &quot;GetProfileString&quot; function.