VBA 64bit & 32bit

VBA

私の周りのExcel環境が 64bit版 が主流になってきている。

そして、ついに 私も Excelの32bit 版 の インストールされている PCが 無くなった。 てか 64bitだけに うっかりしてしまった。 (笑

VBAで 32bit版 での動作テストが。。。 まぁ いいか! (笑
必要な時があれば 再インストールです。

条件コンパイルも 最近は全然 使ってないし。。。 一応 覚書

#If VBA7 Then
    'Office2010以降で64/32bit共通の宣言
    #If Win64 Then
        ' 64bit専用の宣言:分ける必要がある時
    #Else
        ' 32bit専用の宣言:分ける必要がある時
    #End If
#Else
    'Office2007以前用の宣言
#End If

ん? Office2007は サポート終わったし。。

新しいAPIの記述方法や 条件コンパイルが必要な API は MSサイトからダウンロードできる「Win32API_PtrSafe.txt」に載ってます。

公式:https://www.microsoft.com/en-us/download/details.aspx?id=9970

インストール 面倒なので 予備の保管場所として ここにアップしておく(いいのか? ) Win32API_PtrSafe.txt

実際の利用としては、こんな 感じ

#If VBA7 Then
    Dim mXLHwnd As LongPtr    'Excel's window handle
    Dim mhwndForm As LongPtr  'userform's window handle
    Private Declare PtrSafe Function FindWindowA Lib "user32" ( _
                                        ByVal lpClassName As String, _
                                        ByVal lpWindowName As String _
                                        ) As LongPtr
    #If Win64 Then
        Private Declare PtrSafe Function SetWindowLongA Lib "user32" Alias "SetWindowLongPtrA" ( _
                                        ByVal hwnd As LongPtr, _
                                        ByVal nIndex As Long, _
                                        ByVal dwNewLong As LongPtr _
                                        ) As LongPtr
    #Else
        Private Declare PtrSafe Function SetWindowLongA Lib "user32" ( _
                                        ByVal hwnd As LongPtr, _
                                        ByVal nIndex As Long, _
                                        ByVal dwNewLong As LongPtr _
                                        ) As LongPtr
    #End If
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
    Dim mXLHwnd As Long    'Excel's window handle
    Dim mhwndForm As Long  'userform's window handle
    Private Declare Function FindWindowA Lib "user32" ( _
                                        ByVal lpClassName As String, _
                                        ByVal lpWindowName As String _
                                        ) As Long
    Private Declare Function SetWindowLongA Lib "user32" ( _
                                        ByVal hwnd As Long, _
                                        ByVal nIndex As Long, _
                                        ByVal dwNewLong As Long _
                                        ) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#End If

 
64bit版は 基本 32bit版 に下記の変更を加えます。

  • Declare の後に PtrSafe を挿入する。
  • ウィンドウハンドル、ポインタを表す関数の引数、戻り値の型は、Long型からLongPtr型に変更する。

で、64bit に対応です。
 

LongPtr データ型

<MSの説明>
LongPtr は、32 ビット環境では Long に変換され、64 ビット環境では LongLong に変換されるので、実際のデータ型ではありません。LongPtr を使用すると、32 ビット環境と 64 ビット環境の両方で実行できる移植性のあるコードを作成できます。ポインターおよびハンドラーには LongPtr を使用してください。

外部サイト

Office の 32 ビット バージョンと 64 ビット バージョン間の互換性:
https://docs.microsoft.com/ja-jp/office/client-developer/shared/compatibility-between-the-32-bit-and-64-bit-versions-of-office

LongPtr データ型:
https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/longptr-data-type

コメント

タイトルとURLをコピーしました