ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error using Word object library in Excel (VBA) (https://www.excelbanter.com/excel-programming/324563-error-using-word-object-library-excel-vba.html)

LukeCarvey

Error using Word object library in Excel (VBA)
 
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.

NickHK

Error using Word object library in Excel (VBA)
 
Luke,
VBA does not have a "System" object. If you are something .Net, that's
another thing.
Anyway, try:
http://www.exceltip.com/st/Private_P...iles_using_VBA
_in_Microsoft_Excel/490.html

NickHK

"LukeCarvey" wrote in message
...
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.




Bob Phillips[_6_]

Error using Word object library in Excel (VBA)
 
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)


"NickHK" wrote in message
...
Luke,
VBA does not have a "System" object. If you are something .Net, that's
another thing.
Anyway, try:

http://www.exceltip.com/st/Private_P...iles_using_VBA
_in_Microsoft_Excel/490.html

NickHK

"LukeCarvey" wrote in message
...
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.






AA2e72E

Error using Word object library in Excel (VBA)
 
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

"LukeCarvey" wrote:

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.



All times are GMT +1. The time now is 09:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com