Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrting values to an ini file

Status
Not open for further replies.

fowlerontherun

Programmer
Nov 2, 2012
5
GB
Hi guys,

i am currently trying to write a new macro for my users. I need to have two buttons that basically just print to a defined printer. That part is easy.

One of these buttons needs to write the value #letterhead# to a .ini file whenever it prints and the other has to write remove that value when it prints.

has anyone got any advice on the best way to do this?
 
my code is as follows.

[highlight #D3D7CF]Public Function IniFileName() As String
IniFileName = "c:\test\copitrak.ini"
End Function




Private Function WriteIniFileString(ByVal Sect As String, ByVal Keyname As String, ByVal Wstr As String) As String
Dim Worked As Long

iNoOfCharInIni = 0
sIniString = ""
If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
Else
Worked = WritePrivateProfileString(Sect, Keyname, Wstr, IniFileName)
If Worked Then
iNoOfCharInIni = Worked
sIniString = Wstr
End If
WriteIniFileString = sIniString
End If
End Function[/highlight]

All i want to do is parse the function with the following parameters.
any ideas

WriteIniFileString("TRAY", "Paper", "letterhead")
 
Use the WritePrivateProfileString API call. The VB definition is

Code:
Public Declare Function WritePrivateProfileString _
        Lib "kernel32" Alias "WritePrivateProfileStringA" _
            (ByVal lpSectionName As String, _
             ByVal lpKeyName As Any, _
             ByVal lpString As Any, _
             ByVal lpFileName As String) As Long

To add a value to the file
Code:
Call WritePrivateProfileString(SectionName, _
                                   KeyName, _
                                   Value, _
                                   IniFile)

and to remove one

Code:
Call WritePrivateProfileString(SectionName, _
                                   KeyName, _
                                   vbNullString, _
                                   IniFile)
 
im lost.

so how would i call the following

section = TRAY
Keyname = Paper
Value = Letterhead

 
Your code looks correct except that I don't see where [blue]IniFileName[/blue] is defined.

I assume that you mean
Code:
section = [red]"[/red]TRAY[red]"[/red]
Keyname = [red]"[/red]Paper[red]"[/red]
Value = [red]"[/red]Letterhead[red]"[/red]

That should create a IniFile that looks like
Code:
[TRAY]
Paper = Letterhead

I would modify your routine to
Code:
Private Function WriteIniFileString(ByVal Sect As String, _
                                    ByVal Keyname As String, _
                                    Optional ByVal Wstr As String = "") As String

Dim Worked                     As Long

iNoOfCharInIni = 0
sIniString = ""

If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
Else
    If Trim$(Wstr) = "" Then
        [blue]' Delete the entry if the value is blank.[/blue]
        Worked = WritePrivateProfileString(Sect, Keyname, vbNullChar, IniFileName)
    Else
        [blue]' Save the entry[/blue]
        Worked = WritePrivateProfileString(Sect, Keyname, Wstr, IniFileName)
    End If
    If Worked Then
        iNoOfCharInIni = Worked
        sIniString = Wstr
    End If
    WriteIniFileString = sIniString
End If
End Function

I don't know what the variables [blue]iNoOfCharInIni[/blue] and [blue]sIniString[/blue] are all about. They aren't declared.
 
ive sorted it now. was just a matter of getting my head round it. ive goen with the following.

Public Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


Sub Headed()

Call WritePrivateProfileString("TRAY", _
"DocumentName", _
"#Letterhead#", _
"c:\test\copitrak.ini")

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub

Sub Plain()
Call WritePrivateProfileString("TRAY", _
"DocumentName", _
vbNullString, _
"c:\test\copitrak.ini")

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top