Excel VBA不兼容kernel32调用

我在VBA中的下面的代码在Excel 2003中工作得很好。将模板迁移到Excel 2007不起作用。

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, 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 String, ByVal lpString As Any, ByVal lpFileName As String) As Long Private Sub Workbook_Open() Exit Sub Dim WorksheetName As String Dim WorksheetCell As String Dim Section As String Dim kKey As String Dim lLine As Long Dim InvoiceNumber As Long Dim InvoiceNumberCell As Object Dim TemplateName As String Dim IniFileName As String Dim Dummy As Variant TemplateName = "MyInvoicesTemplate.xlt" WorksheetName = "Invoice" WorksheetCell = "H2" Section = "Invoice" kKey = "Number" IniFileName = "C:\Windows\Temp\InvoiceNumber.txt" Set InvoiceNumberCell = Worksheets(WorksheetName).Range(WorksheetCell) If UCase(ActiveWorkbook.Name) = UCase(TemplateName) Then GoTo Finito Dummy = GetString(Section, kKey, IniFileName) If Left(Dummy, 1) = Chr$(0) Then InvoiceNumber = 1 Else InvoiceNumber = CLng(Dummy) + 1 End If WritePrivateProfileString Section, kKey, CStr(InvoiceNumber), IniFileName InvoiceNumberCell.Value = InvoiceNumber With ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule lLine = .ProcBodyLine("Workbook_Open", vbext_pk_Proc) .InsertLines lLine + 1, "Exit Sub" End With Finito: Set InvoiceNumberCell = Nothing End Sub Function GetString(Section As String, Key As String, File As String) As String Dim KeyValue As String Dim Characters As Long KeyValue = String(255, 0) Characters = GetPrivateProfileString(Section, Key, "", KeyValue, 255, File) If Characters > 1 Then KeyValue = Left(KeyValue, Characters) End If GetString = KeyValue End Function 

任何想法为什么会发生? 我试图以不同的格式保存模板,但没有运气!

谢谢。 MK

如果您使用的是64位Office,则API已更改。 看到

http://www.jkp-ads.com/articles/apideclarations.asp

API已经改变。

IF VBA7,声明ptrSafeLongPtr