1

我在 Access 2010 中的 VBA 代码有问题。我声明了一个类型,但是当我尝试获取这种类型的变量的指针(使用函数 VarPtr)时,我得到一个compilation error: incompatible type.

Dim dm As DEVMODE
Dim pd As PRINTER_DEFAULTS

pd.pDevMode = VarPtr(dm) ' the line that throws the error

' pDevMOde is a Long that is supose to contain the adress of the dm variable

Private Type DEVMODE
  dmDeviceName As String * CCHDEVICENAME ' (1 To CCHDEVICENAME) As Byte
  dmSpecVersion As Integer
  dmDriverVersion As Integer
  dmSize As Integer
  dmDriverExtra As Integer
  dmFields As Long
  dmOrientation As Integer
  dmPaperSize As Integer
  dmPaperLength As Integer
  dmPaperWidth As Integer
  dmScale As Integer
  dmCopies As Integer
  dmDefaultSource As Integer
  dmPrintQuality As Integer
  dmColor As Integer
  dmDuplex As Integer
  dmYResolution As Integer
  dmTTOption As Integer
  dmCollate As Integer
  dmFormName As String * CCHFORMNAME '(1 To CCHFORMNAME) As Byte
  dmUnusedPadding As Integer
  dmBitsPerPel As Integer
  dmPelsWidth As Long
  dmPelsHeight As Long
  dmDisplayFlags As Long
  dmDisplayFrequency As Long
End Type

问题是此代码是在早期版本的 Access 中创建的,并且在 2003 及以下版本中完美运行。我不知道为什么它在 Access 2010 中不起作用。我查看了 2010 和 2003 版本之间的差异,但我没有找到与此相关的任何内容。

有人有线索吗?

谢谢!

4

1 回答 1

3

我终于找到了问题所在。64位版本实际上是有区别的。VarPtr 不再返回 Long 类型,它返回 LongPtr 类型变量,我的代码无法将 LongPtr 放入 Long

参考这里:http: //msdn.microsoft.com/en-us/library/ee691831.aspx

变量指针 | 变体转换器。| 在 64 位版本上返回 LongPtr,在 32 位版本(4 字节)上返回 Long。

希望它可以帮助别人!

于 2012-12-28T15:02:14.440 回答