I am coding a VBA function in Excel with the following code that keeps giving me an error "Argument not optional" when I try to compile it.
The error happens on the call to WriteToINI.
Any suggestions?
Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA"
(ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileString
A" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Long
Sub AutoNew()
Order = GetFromINI("MacroSettings"
, "Order", "C:\dbase\Settings.txt")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
' x = WriteToINI("MacroSettings"
, "Order", "C:\dbase\settings.txt")
ActiveSheet.Range("Order")
.Value = Order
ActiveSheet.SaveAs Filename:="path" & Format(Order, "00#")
End Sub
'--This function will get the value from the ini
' ex: Text1.Text = GetFromINI("Category1","Pa
ram1","C:\
MyINIFile.
ini")
Function GetFromINI(Category As String, Parameter As String, FilePath As String) As String
Dim n As String
n = String(255, Chr(0))
GetFromINI = Left(n, GetPrivateProfileString(Ca
tegory, ByVal Parameter, "", n, Len(n), FilePath)) 'set string value of GetFromINI
If GetFromINI = "" Then
GetFromINI = "N/A"
End If
End Function
'--This function will set the value of a category, parameter, and value into an INI file
' ex: WriteToINI("Category1","Pa
ram1","C:\
MyINIFile.
ini")
Function WriteToINI(Category As String, Parameter As String, Value As String, FilePath As String)
r = WritePrivateProfileString(
Category, Parameter, Value, FilePath)
If r <> 1 Then 'if r <> 1 an error has occured
x = MsgBox("Error: Writing to INI file failed! Check the file path!", vbCritical, "Error")
End If
WriteToIt = r
End Function
Start Free Trial