Using Windows Calls to Write to a Private Text File

ID: 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 "as is" 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 "Section", "Printer", "Text to Add",
 "C:\winword\text.ini"
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 \ 
   "kernel"(lpApplicationName$, lpKeyName$, lpString$, lpFileName$)\ 
   As Integer

   Sub MAIN
   a = WritePrivateProfileString("Section", "Printer", \ 
   "Text to Add", "C:\WINWORD\TEXT.INI")
   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 "SetProfileString" 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


REFERENCES

Microsoft Windows Software Development Kit, Vol. 1, page 4-462 through page 4-464

Additional query words: GetPrivateProfileString 2.0 winword2 word7 word6 winword 7.0 word95 ntword wordnt

Keywords :
Version : WINDOWS:2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a; winnt:6.0
Platform : WINDOWS winnt
Issue type :


Last Reviewed: September 24, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.