Microsoft KB Archive/81792

{|
 * width="100%"|

Using Windows Calls to Write to a Private Text File

 * }

Q81792

-

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 95, versions 7.0, 7.0a
 * Microsoft Word for Windows NT, version 6.0

-

SUMMARY
In Microsoft Word for Windows version 2.x, you can use the Microsoft Windows WritePrivateProfileString call within a WordBasic macro to write to an .INI file other than the WIN.INI file.

The SetPrivateProfileString command was added to Word version 6.0. Use SetPrivateProfileString to define or redefine a setting in a private settings file. A settings file is a text file like WIN.INI that your macros can use for storing and retrieving settings.

Word for Windows 6.0, 7.0
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.

The following is an example for SetPrivateProfileString.

Sub MAIN SetPrivateProfileString &quot;Section&quot;, &quot;Printer&quot;, &quot;Text to Add&quot;, &quot;C:\winword\text.ini&quot; End Sub The above macro writes out the following information to the TEXT.INI file in the C:\WINWORD directory:

  [Section] Printer=Text to Add The SetPrivateProfileString function copies a character string into the specified section of the specified initialization file. The return value is TRUE (1) if the function is successful or FALSE (0) if an error occurs.

Word for Windows 2.x
Using Windows calls to read and write to a private text file is an alternative to the following standard WordBasic file input/output statements and functions:

  Close          Open...For...As   Eof      Print Input     Read Input$      Seek Lof     Write According to the Microsoft Windows Software Development Kit documentation, the WritePrivateProfileString function has the following parameters:

  BOOL WritePrivateProfileString(lpszSectionName, lpszKeyName,   lpszString, lpszFileName)

LPCSTR lpszSectionName; section name LPCSTR lpszKeyName;     keyname LPCSTR lpszString;      string to add LPCSTR lpszFileName;    initialization file name

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

  Declare Function WritePrivateProfileString Lib \ &quot;kernel&quot;(lpApplicationName$, lpKeyName$, lpString$, lpFileName$)\ As Integer

Sub MAIN a = WritePrivateProfileString(&quot;Section&quot;, &quot;Printer&quot;, \   &quot;Text to Add&quot;, &quot;C:\WINWORD\TEXT.INI&quot;) End Sub The above WordBasic macro writes out the following information to the TEXT.INI file in the C:\WINWORD directory:

  [Section] Printer=Text to Add The WritePrivateProfileString function copies a character string into the specified section of the specified initialization file. The return value is TRUE (1) if the function is successful or FALSE (0) if an error occurs.

If the FileName parameter does not contain a fully-qualified path for the file, this function searches the Windows directory for the file. If the file does not exist, this function creates the file in the WINDOWS directory. If FileName parameter contains a fully-qualified path and the file does not exist, this function creates the file. The specified directory must already exist.

Note: To add a string to the WIN.INI file use the WordBasic &quot;SetProfileString&quot; function.

For information about how to do this in Word 97, please see the following article(s) in the Microsoft Knowledge Base:

"Q160132 Sample VB Code to Set, Retrieve Ini, Registry Settings"