Excel vba GetPrivateProfileString工作2007年,但不是2010年

时间:2014-02-06 19:06:03

标签: excel vba

我声明了以下代码:

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 Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String) As   String
Dim Worked As Long
Dim RetStr As String * 128
Dim StrSize As Long
Dim iNoOfCharInIni As Long
Dim sIniString As String
Dim sProfileString As String

iNoOfCharInIni = 0
sIniString = ""
If Sect = "" Or Keyname = "" Then
   MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
Else
   sProfileString = ""
RetStr = Space(128)
StrSize = Len(RetStr)
Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)
   If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Left$(RetStr, Worked)
   End If
 End If
ReadIniFileString = sIniString
End Function

这适用于2007年,但我在Excel 2010上收到错误:

Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)

我得到一个“无法找到子或函数错误”我在网上看到我应该能够通过在PtrSafe中声明函数并返回LongPtr来解决这个问题。我已经做到了,但得到了相同的结果!

请帮忙!

非常感谢!

拉​​斯

2 个答案:

答案 0 :(得分:2)

要在64位版本的Excell中使用此功能,您需要将PtrSafe属性添加到函数声明中,如下所示:

Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias...

答案 1 :(得分:0)

您没有说Excel 2010是32位还是64位。我不确定如果在64位Excel实例中运行的VBA模块尝试调用32位例程会发生什么情况,并且kernel32.dll是一个32位的库。由于不推荐使用GetPrivatePorfileString,我怀疑它没有被移植到kernel32.dll的64位副本。