Error using Word object library in Excel (VBA)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to use the PrivateProfileString() function contained within the
'System' object to read/write an .ini file. I have referenced the relevant
Word object library to Excel but the function only works whilst Word is open.
I get the following (common) error:

Run-time Error 429; ActiveX component can't create object.

Can I actually read/write an .ini file using VBA in Excel? Would be greatful
for any help.
 
You can use APIs in Excel




Private Declare Function GetPrivateProfileInt Lib "kernel32" _
Alias "GetPrivateProfileIntA"
_
(ByVal lpApplicationName As
String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String)
As Long

Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias
"GetPrivateProfileStringA" _
(ByVal lpApplicationName As
String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String,
_
ByVal lpReturnedString As
String, _
ByVal nSize As Long, _
ByVal lpFileName As
String) As Long

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

and use like so

cSubItems = GetPrivateProfileInt("myApp", "Id", 0, Filename)

and

Dim sKeyValue As String
Dim nLen As Long

sKeyValue = Space$(255)
nLen = GetPrivateProfileString(AppId, K"myApp","Id", "Not Found", _
sKeyValue, Len(sKeyValue), Filename)
If nLen = Len(sKeyValue) - 1 Then
'means sKeyValue not long enough
sKeyValue = Space$(Len(sKeyValue) + 100)
Else
sKeyValue = Left(sKeyValue, nLen)
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
You can read fromINI files with this:

Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As
String, ByVal nSize As Long, ByVal lpFileName As String) As Long
sub xx()
Ret = String(255, 0)
Length = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret,
255, "C:\myfile.ini")
Ret = Left$(Ret, Length) ' assumes Length<>0
end Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top