<状況>
下記、ウインドウを最前面化し全画面化するVBAコードについて。
Officeバージョンアップデートに伴い、Excelが32bit⇒64bitに変更になったため、
32bit版の際に正常動作していたコード(「修正前のコード」参照)を実行すると、
変数hwndの型不一致のエラーが出るようになりました。
そのため、下記の修正をしました(「修正後のコード」参照)
・DeclareステートメントにPtrSafe属性を追加
・Long型をLongPtr型に変更
修正後、無事、正常動作が確認できましたが、下記の疑問が生まれました。
<知りたいこと>
・変数hwndの値は一体何の数字を示している?
下記の■でdebug.printで出力させてみたところ、「2890758」というような数値がでます。
・「2890758」であれば、そもそもLong型の整数に対応している気がしますが、(https://www.tipsfound.com/vba/02008)
なぜ64bit環境では、hwnd(「2890758」)とLong型で不一致が起きるのでしょう。
(LongPtrに書きかえて、わざわざLongLong型にさせなければいけない理由とは・・)
(修正前のコード)
Option Explicit Declare Function isiconic Lib "user32" Alias "IsIconic" (ByVal hwnd As Long) As Long Declare Function ShowWindowAsync Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Sub Sample(ie As InternetExplorerMedium) If isiconic(ie.hwnd) Then ShowWindowAsync ie.hwnd, &H9 End If SetForegroundWindow (ie.hwnd) ShowWindowAsync ie.hwnd, &H3 End Sub ``` (修正後のコード)
Option Explicit
Declare PtrSafe Function isiconic Lib "user32" Alias "IsIconic" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function ShowWindowAsync Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As LongPtr) As Long
Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Sub Sample(ie As InternetExplorerMedium)
Debug.Print TypeName(ie.hwnd) ■
Debug.Print ie.hwnd
If isiconic(ie.hwnd) Then ShowWindowAsync ie.hwnd, &H9 End If SetForegroundWindow (ie.hwnd) ShowWindowAsync ie.hwnd, &H3
End Sub

回答2件
あなたの回答
tips
プレビュー