Table of contents
TOC
折叠目录
展开目录

Declare 语句Declare Statement

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

模块级用于声明对动态链接库(DLL) 中的外部过程的引用。Used at module level to declare references to externalprocedures in adynamic-link library (DLL).

请注意 声明语句使用PtrSafe关键字是建议使用的语法。声明包括PtrSafe工作正确 VBA7 开发环境在 32 位和 64 位平台上,只在需要存储 64 位Declare语句 (参数和返回值) 中的所有数据类型之后的语句更新使用LongLong 64 位积分或LongPtr指针和句柄数量。确保向后兼容使用 VBA 版本 6 及更早版本,请使用下面的结构:Note Declare statements with the PtrSafe keyword is the recommended syntax. Declare statements that include PtrSafe work correctly in the VBA7 development environment on both 32-bit and 64-bit platforms only after all data types in the Declare statement (parameters and return values) that need to store 64-bit quantities are updated to useLongLong for 64-bit integrals orLongPtr for pointers and handles. To ensure backwards compatibility with VBA version 6 and earlier use the following construct:

#If VBA7 Then 
Declare PtrSafe Sub... 
#Else 
Declare Sub... 
#EndIf

语法 1[公用 |专用]声明名称Lib"libname"[别名' 'aliasname'' ][ ( [ arglist ] ) ]语法 2[公用 |专用]声明函数名称Lib"libname"[别名' 'aliasname'' ][ ( [ arglist ] ) ][类型]VBA7 声明语句语法Syntax 1 [ Public |Private ] DeclareSubnameLib"libname" [ Alias"aliasname" ] [ ( [ arglist ] ) ] Syntax 2 [ Public |Private ] DeclareFunctionnameLib"libname" [ Alias"aliasname" ] [ ( [ arglist ] ) ] [ Astype ] VBA7 Declare Statement Syntax

请注意 代码运行在 64 位版本的 Microsoft Office 中所有声明必须都包含PtrSafe关键字,并在需要存储 64 位Declare语句 (参数和返回值) 中的所有数据类型必须都更新,以使用对于 64 位的整型或指针以及句柄LongPtrLongLongNote For code to run in 64-bit versions of Microsoft Office all Declare statements must include the PtrSafe keyword, and all data types in the Declare statement (parameters and return values) that need to store 64-bit quantities must be updated to useLongLong for 64-bit integrals orLongPtr for pointers and handles.

语法 1 (子)[公用 |专用]声明PtrSafe * * 子名称Lib * * ' 'libname'' [别名' 'aliasname'' ] [ ( [ arglist ] ) ]语法 2 (函数) [公共 |专用]声明PtrSafe函数名称Lib ' 'libname'' [别名' 'aliasname'' ] [( [ arglist ] ) ][类型]Syntax 1 (Sub) [ Public |Private ] DeclarePtrSafe ** SubnameLib** "libname" [ Alias"aliasname" ] [ ( [ arglist ] ) ] Syntax 2 (Function) [ Public |Private ] DeclarePtrSafeFunctionnameLib"libname" [ Alias"aliasname" ] [ ( [ arglist ] ) ] [ Astype ]

部件Part说明Description
公共Public可选。用于声明供所有模块中的所有其它过程的过程。Optional. Used to declare procedures that are available to all other procedures in all modules.
私有Private可选。用于声明可仅在模块中的过程进行声明的位置。Optional. Used to declare procedures that are available only within the module where the declaration is made.
PtrSafePtrSafe在 64 位平台上被必需。PtrSafe关键字断言Declare语句是安全地运行在 64 位版本的 Microsoft Office 中Required on 64-bit. The PtrSafe keyword asserts that a Declare statement is safe to run in 64-bit versions of Microsoft Office
Sub可选 (子程序函数必须显示)。指示该过程不返回值。Optional (either Sub or Function must appear). Indicates that the procedure doesn't return a value.
函数Function可选 (子程序函数必须显示)。指示过程返回一个值,可以使用表达式中。Optional (either Sub or Function must appear). Indicates that the procedure returns a value that can be used in anexpression.
namename必需。任何有效的过程名称。请注意,DLL 入口点区分大小写。Required. Any valid procedure name. Note that DLL entry points are case sensitive.
LibLib必填。指示 DLL 或代码资源中包含所声明的过程。Lib子句是必需的所有声明。Required. Indicates that a DLL or code resource contains the procedure being declared. The Lib clause is required for all declarations.
libnamelibname必需。包含已声明过程的 DLL 或代码资源。Required. Name of the DLL or code resource that contains the declared procedure.
别名Alias可选。指示所调用的过程在 DLL 中具有另一个名称。外部过程名称后与关键字相同,则这非常有用。当 DLL 过程具有同一作用域内同名公共变量常量、 或任何其他过程,还可以使用别名别名也是 DLL 过程名称中的任何字符不允许使用通过 DLL 命名约定的情况下很有用。Optional. Indicates that the procedure being called has another name in the DLL. This is useful when the external procedure name is the same as a keyword. You can also use Alias when a DLL procedure has the same name as a publicvariable, constant, or any other procedure in the same scope. Alias is also useful if any characters in the DLL procedure name aren't allowed by the DLL naming convention.
aliasnamealiasname可选。DLL 或代码资源中的过程名称。如果第一个字符不是一个数字符号 ( # ), aliasname_是过程的 DLL 中的入口点的名称。如果 ( # ) 是第一个字符,后面的所有字符必须都表明过程的入口点的序号。Optional. Name of the procedure in the DLL or code resource. If the first character is not a number sign ( # ), _aliasname is the name of the procedure's entry point in the DLL. If ( # ) is the first character, all characters that follow must indicate the ordinal number of the procedure's entry point.
arglistarglist可选。当它被调用时传递给该过程的变量表示参数的列表。Optional. List of variables representing arguments that are passed to the procedure when it is called.
类型type可选。Function过程; 所返回值的数据类型可能是字节布尔值整数LongLongLongPtr货币十进制(目前不支持),日期字符串(可变长度),或variant 类型的值用户定义的类型对象类型。( LongLong是有效的声明类型仅在 64 位平台上)。Optional. Data type of the value returned by a Function procedure; may beByte, Boolean, Integer, Long, LongLong, LongPtr, Currency, Single, Double, Decimal (not currently supported),Date, String (variable length only), orVariant, a user-defined type, or an object type. ( LongLong is a valid declared type only on 64-bit platforms.)
_Arglist_参数具有以下语法和部件: [可选] [ ByValByRef ] [] varname [ () ] [类型]The arglist argument has the following syntax and parts: [ Optional ] [ ByVal
部件Part说明Description
可选Optional可选。指示参数不是必需的。如果使用, arglist_中的所有后续参数也必须为可选的并且声明使用的可选关键字。如果使用参数数组,则不使用任何参数可选Optional. Indicates that an argument is not required. If used, all subsequent arguments in _arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
ByValByVal可选。指示该参数通过值传递。Optional. Indicates that the argument is passed by value.
引用传递ByRef指示该参数通过引用传递。 ByRef是 Visual Basic 中的默认值。Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.
参数数组ParamArray可选。仅用作_arglist_中的最后一个参数用于指示最后的参数是可选数组变量元素。参数组关键字使您可以提供任意数量的参数。参数组关键字不能使用ByValByRef可选Optional. Used only as the last argument in arglist to indicate that the final argument is an Optionalarray of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. The ParamArray keyword can't be used with ByVal, ByRef, or Optional.
varnamevarname必需。表示要传递到过程的参数的变量的名称;遵循标准变量命名约定。Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions.
( )对于数组变量被必需。指示该_varname_是数组。Required for array variables. Indicates that varname is an array.
类型type可选。数据类型的参数传递给该过程;可能是字节布尔值整数LongLongLongPtr货币十进制(目前不支持)、日期字符串(可变长度仅),对象变量、 用户定义的类型或对象类型。( LongLong是有效的声明类型仅在 64 位平台上)。Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, LongLong, LongPtr, Currency, Single, Double, Decimal (not currently supported), Date, String (variable length only), Object, Variant, a user-defined type, or an object type. ( LongLong is a valid declared type only on 64-bit platforms.)

备注有关函数的过程,过程的数据类型确定它会返回数据类型。按照_arglist_ As子句可用于指定该函数的返回类型。在_arglist_中,您可以使用As子句指定数据类型参数传递给该过程的任何。除了指定任何标准的数据类型,您可以指定为任何arglist_抑制类型检查并允许任何要传递给该过程的数据类型中。空括号表示的子程序函数的过程有没有参数和 Visual Basic 应确保任何被传递。在以下示例中,First不采用任何参数。如果您使用参数在对不采用任何参数。如果您使用参数对First,则会发生错误:Remarks For Function procedures, the data type of the procedure determines the data type it returns. You can use an As clause following _arglist to specify the return type of the function. Within arglist, you can use an As clause to specify the data type of any of the arguments passed to the procedure. In addition to specifying any of the standard data types, you can specify As Any in arglist to inhibit type checking and allow any data type to be passed to the procedure. Empty parentheses indicate that the Sub or Function procedure has no arguments and that Visual Basic should ensure that none are passed. In the following example, First takes no arguments. If you use arguments in a call to takes no arguments. If you use arguments in a call to First, an error occurs:

Declare Sub First Lib "MyLib" () 

如果您包括一个参数列表,编号和参数的类型检查每次被调用的过程。在下面的示例中,使用一个的参数:If you include an argument list, the number and type of arguments are checked each time the procedure is called. In the following example, takes one Long argument:

Declare Sub First Lib "MyLib" (X As Long) 

请注意 不能在参数列表中声明语句; 有固定长度的字符串只有可变长度字符串可以传递给过程。固定长度的字符串可以显示为过程的参数,但在传递之前转换为可变长度的字符串。Note You can't have fixed-length strings in the argument list of a Declare statement; only variable-length strings can be passed to procedures. Fixed-length strings can appear as procedure arguments, but they are converted to variable-length strings before being passed.

请注意VbNullString的常数使用时调用外部过程,其中外部过程都需要一个字符串,其值为零。这不是同样为零长度字符串 ("")。Note The vbNullString constant is used when calling external procedures, where the external procedure requires a string whose value is zero. This is not the same thing as a zero-length string ("").

示例Example

此示例演示如何声明语句用于在标准模块的模块级别声明对动态链接库 (DLL) 中的外部过程的引用。如果Declare语句也是专用的可以将Declare语句放在类模块中。This example shows how the Declare statement is used at the module level of a standard module to declare a reference to an external procedure in a dynamic-link library (DLL). You can place the Declare statements in class modules if the Declare statements are Private.

注意Note

' In Microsoft Windows (16-bit): 
Declare Sub MessageBeep Lib "User" (ByVal N As Integer) 
' Assume SomeBeep is an alias for the procedure name. 
Declare Sub MessageBeep Lib "User" Alias "SomeBeep"(ByVal N As Integer) 
' Use an ordinal in the Alias clause to call GetWinFlags. 
Declare Function GetWinFlags Lib "Kernel" Alias "#132"()As Long 

' In 32-bit Microsoft Windows systems, specify the library USER32.DLL, 
' rather than USER.DLL. You can use conditional compilation to write 
' code that can run on either Win32 or Win16. 
#If Win32 Then 
    Declare Sub MessageBeep Lib "User32" (ByVal N As Long) 
#Else 
    Declare Sub MessageBeep Lib "User" (ByVal N As Integer) 
#End If 


' 64-bit Declare statement example: 
Declare PtrSafe Function GetActiveWindow Lib "User32" () As LongPtr 

' Conditional Compilation Example 
#If Vba7 Then 
     ' Code is running in  32-bit or 64-bit VBA7. 
     #If Win64 Then 
          ' Code is running in 64-bit VBA7. 
     #Else 
          ' Code is not running in 64-bit VBA7. 
     #End If 
#Else 
     ' Code is NOT running in 32-bit or 64-bit VBA7. 
#End If 

© 2018 Microsoft