【问题标题】:Excel vba GetPrivateProfileString working 2007, but not 2010Excel vba GetPrivateProfileString 工作 2007,但不是 2010
【发布时间】:2014-03-03 22:05:02
【问题描述】:

我声明了以下代码:

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 来解决此问题。我已经这样做了,但得到了相同的结果!

请帮忙!

非常感谢!

罗斯

【问题讨论】:

  • 您是否尝试从您的声明中删除private

标签: excel vba


【解决方案1】:

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

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

【讨论】:

  • 我可以确认这个解决方案确实适用于这个功能。
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
相关资源
最近更新 更多