Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Anzeige
Inhaltsverzeichnis

Anfrage zu 32 und 64 bit

Forumthread: Anfrage zu 32 und 64 bit

Anfrage zu 32 und 64 bit
07.04.2025 14:09:19
chris b.
Hallo zusammen, ich bitte um Hilfe. Ich habe eine ältere Datei gefunden die unter 32 Bit gelaufen ist.
Könnt ihr mir helfen das dieses Programm auch unter Windows 11 64 Bit läuft ?
Vielen Lieben Dank!
Hier der Code :


Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long


Sub auswahl()
UserForm1.Show
End Sub


Sub vordergrund()
cb = Cells(1, 1)

whandle = FindWindow(vbNullString, cb)
Call SetWindowPos(whandle, -1, 0, 0, 0, 0, 3)
AppActivate (cb)
'Unload UserForm1
'UserForm1.Hide

End Sub


Sub hintergrund()
cb = Cells(1, 1)
whandle = FindWindow(vbNullString, cb)
Call SetWindowPos(whandle, -2, 0, 0, 0, 0, 3)
End Sub
Anzeige

7
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Anfrage zu 32 und 64 bit
07.04.2025 14:26:46
MCO
Ungeprüft:

Antwort von der freundlichen KI:

Damit dein 32-Bit-VBA-Code unter Windows 11 (64-Bit) und in 64-Bit-Office (z. B. Excel) korrekt funktioniert, musst du API-Deklarationen anpassen. Das liegt daran, dass „Long“ in 32-Bit-Versionen 32-Bit-Adressen enthält, in 64-Bit aber „LongPtr“ bzw. „LongLong“ notwendig ist.

Hier ist dein überarbeiteter VBA-Code, kompatibel mit 64-Bit Office / Windows 11:

#If VBA7 Then

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

Private Declare PtrSafe Function SetWindowPos Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
#End If


Sub auswahl()
UserForm1.Show
End Sub
Sub vordergrund()

Dim cb As String
cb = Cells(1, 1).Value

Dim whandle As LongPtr
whandle = FindWindow(vbNullString, cb)

Call SetWindowPos(whandle, -1, 0, 0, 0, 0, 3)
AppActivate cb
End Sub
Sub hintergrund()

Dim cb As String
cb = Cells(1, 1).Value

Dim whandle As LongPtr
whandle = FindWindow(vbNullString, cb)

Call SetWindowPos(whandle, -2, 0, 0, 0, 0, 3)
End Sub

Was wurde geändert?
#If VBA7 Then sorgt für Kompatibilität mit 64-Bit-Office (ab Office 2010).

PtrSafe ist erforderlich für 64-Bit-Kompatibilität.

LongPtr wird verwendet für Zeiger/Rückgabewerte von Handles, die in 64-Bit größer sind als Long.

Gruß, MCO
Anzeige
AW: Anfrage zu 32 und 64 bit
07.04.2025 14:36:10
volti
Hallo MCO,

korrekterweise sollte das Handle whandle auch entsprechend angepasst werden....

Altes Excel VBA6 32 Bit erfordert Long, neues Excel VBA7 32/63 Bit erfordert LongPtr und neues Excel 64 Bit kann optional auch LongLong bei Declare-Anpassung verwenden

Gruß KH
Anzeige
AW: Anfrage zu 32 und 64 bit
07.04.2025 15:00:59
chris b.
Hallo ihr lieben, ich komme leider nicht mehr alleine klar, habe noch etwas gefunden was nicht kompatibel ist.
Public x1 As String

Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Dim m1

Public cb As String
Dim whandle


Vielen Dank !
Anzeige
AW: Anfrage zu 32 und 64 bit
07.04.2025 15:23:01
volti
Hallo Chris,

hier die Umsetzung (ngetestet)....

Bei vielen Umsetzungen hilft oft auch ein API-Viewer. Hier meiner zur evtl. Ansicht
https://www.clever-excel-forum.de/Thread-API-Viewer-Update
Bei Bedarf und/oder Problemen kann ich ihn auch hier einstellen.


#If VBA7 Then

Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
#If Win64 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long) As LongPtr
#End If
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As LongPtr, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" ( _
ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
#Else
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
#End If


Was viele oft vergessen. Es sind nicht nur die Declares anzupassen, sondern ggf. auch im Code alle Handles und Pointer und ggf. weiteres.

PS: Bei Set/GetWindowlong gibt es eine Unterscheidung bei VBA7 32/64 Bit.
Statt der Unterscheidung nach #IF VBA7 kann man auch die Unterscheidung nach #IF Win64 machen.
Das liegt daran, dass bei VBA7 die 32 Bit-Version sowohl die alten wie auch die neuen Declares unterstützt.

Letztendlich müsste man Deinen gesamten entprechenden Code kennen, um das optimal anpassen zu können.
Aber vielleicht reicht es ja so schon. Viel Erfolg.

#If Win64 Then

Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As LongPtr, ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" ( _
ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
#End If


Gruß KH

Anzeige
AW: Anfrage zu 32 und 64 bit
08.04.2025 00:46:10
chris b.
Vielen Dank euch allen!!!
AW: Anfrage zu 32 und 64 bit
07.04.2025 14:31:45
volti
Hallo Chris,

dieses hier (ungetestet) sollte unter 32 und 64 Bit laufen
#If VBA7 Then

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SetWindowPos Lib "user32" ( _
ByVal hwnd As LongPtr, _
ByVal hWndInsertAfter As LongPtr, _
ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long
Dim hHandle As LongPtr
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Dim hHandle As Long
#End If

Sub auswahl()
UserForm1.Show
End Sub


Sub vordergrund()

cb = Cells(1, 1)

whandle = FindWindow(vbNullString, cb)
Call SetWindowPos(whandle, -1, 0, 0, 0, 0, 3)
AppActivate (cb)
'Unload UserForm1
'UserForm1.Hide

End Sub


Sub hintergrund()
cb = Cells(1, 1)
whandle = FindWindow(vbNullString, cb)
Call SetWindowPos(whandle, -2, 0, 0, 0, 0, 3)
End Sub


Gruß
Karl-Heinz
Anzeige
AW: Anfrage zu 32 und 64 bit
07.04.2025 14:59:10
volti
Upps,

jetzt habe ich mich eben selbst verhauen, sorry.

Dim hHandle As .... muss bei Dir natürlich whandle heißen

Kam daher, weil bei mir Handle immer mit h... beginnen.

Gruß KH
Anzeige
Anzeige
Live-Forum - Die aktuellen Beiträge
Datum
Titel
14.05.2026 13:31:09
14.05.2026 09:50:42
13.05.2026 19:14:18