首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA不兼容kernel32调用

Excel VBA不兼容kernel32调用
EN

Stack Overflow用户
提问于 2012-02-21 04:32:00
回答 2查看 4.2K关注 0票数 1

我用VBA编写了下面的代码,它在Excel2003中工作得很好。将模板迁移到Excel 2007不起作用。

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-21 05:55:23

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

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

票数 3
EN

Stack Overflow用户

发布于 2013-12-18 11:26:27

API已更改。

如果为VBA7,则声明ptrSafeLongPtr,依此类推。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9367992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档