在 Win64 VBA Office 中声明语句 [英] Declare statement in Win64 VBA Office

查看:125
本文介绍了在 Win64 VBA Office 中声明语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 32 位中使用了 vba 代码.现在我已迁移到 Windows 10 64 位,我收到消息应更新此项目中的代码以在 64 位系统上使用.请查看并更新 Declare 语句,然后使用 PtrSafe 属性标记它们."我拥有的声明命令如下:

I have used vba code in Windows 32 bit. Now that I've migrated to Windows 10 64 bit I got the message "The code in this project should be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with PtrSafe attribute." The Declare commands I have are the following:

Public Declare Function GetUserNameEx Lib "Secur32.dll" Alias "GetUserNameExA" ( _
  ByVal NameFormat As EXTENDED_NAME_FORMAT, _
  ByVal lpNameBuffer As String, _
  ByRef lpnSize As Long) As Long

Public Enum EXTENDED_NAME_FORMAT
  NameUnknown = 0
  NameFullyQualifiedDN = 1
  NameSamCompatible = 2
  NameDisplay = 3
  NameUniqueId = 6
  NameCanonical = 7
  NameUserPrincipal = 8
  NameCanonicalEx = 9
  NameServicePrincipal = 10
  NameDnsDomain = 12
End Enum

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long

我查看了一些文档,但在使用 LongLong 和 LongPtr 语句时我无法真正理解.或者如果它只是在 Declare 语句中声明 PtrSafe.

I've looked at some documentation but I can not really understand when using the LongLong and LongPtr statements. Or if it's only declare PtrSafe in the Declare statement.

指向 Microsoft 文档的链接https://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx

Link to MicroSoft documentation https://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx

有人可以帮我吗?

推荐答案

当涉及内存地址时使用 LongPtr,当它只是一个数字时使用 Long.每当您在声明中使用 LongPtr 时,请包括 PtrSafe.所以你的 ShellExecute 声明将被(测试):

You use LongPtr when it concerns a memory address and Long when it is just a number. Include PtrSafe whenever you use a LongPtr in your declaration. So your ShellExecute declaration will be (tested):

#If VBA7 Then
Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hWnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, _
        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
#Else
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  (ByVal hWnd As Long, ByVal lpOperation As String, _
  ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If

这篇关于在 Win64 VBA Office 中声明语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆