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

32 bit auf 64 bit umschreiben

Forumthread: 32 bit auf 64 bit umschreiben

32 bit auf 64 bit umschreiben
05.12.2022 11:02:55
Dogan
Hallo liebe Community,
ich habe mit der hilfe hier im Forum, eine, für mich sehr umfangreiche VBA erstellt und müsste diese aus der 32bit Version in eine 64bit Version umschreiben. Ist das jetzt eher ein "Übersetzen" oder eher eine neue Programmierung die man vornehmen muss?
Z.B. habe ich folgendes in der VBA stehen die in der 64 bit Version nicht mehr funktioniert:
Private Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath _
As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, _
ByVal uFlags As Long) As Long
Anzeige

5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: 32 bit auf 64 bit umschreiben
05.12.2022 11:22:05
Rudi
Hallo,

#If VBA7 Then
Private Declare PtrSafe Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath _
As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, _
ByVal uFlags As Long) As LongPtr
#Else
Private Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath _
As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, _
ByVal uFlags As Long) As Long
#End If
sollte gehen.
Gruß
Rudi
Anzeige
AW: 32 bit auf 64 bit umschreiben
05.12.2022 13:45:03
Dogan
Ok. Bleibt dort nicht mehr hängen.
Kann ich bei der nächste Meldung folgendes tun?
Das ganze:
Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal _
lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal _
hFindFile As Long, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpSystemTime As SYSTEMTIME) As Long
Als:
#If VBA7 Then
Private Declare PtrSafe Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal _
lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As LongPtr
Private Declare PtrSafe Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal _
hFindFile As Long, ByRef lpFindFileData As WIN32_FIND_DATA) As LongPtr
Private Declare PtrSafe Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As LongPtr
Private Declare PtrSafe Function FileTimeToLocalFileTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpLocalFileTime As FILETIME) As LongPtr
Private Declare PtrSafe Function FileTimeToSystemTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpSystemTime As SYSTEMTIME) As LongPtr
#Else
Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal _
lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal _
hFindFile As Long, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32.dll" (ByRef lpFileTime As _
FILETIME, ByRef lpSystemTime As SYSTEMTIME) As Long
#End If
Anzeige
AW: 32 bit auf 64 bit umschreiben
05.12.2022 13:54:15
Nepumuk
Hallo Dogan,
so:

#If Win64 Then
Private Declare PtrSafe Function FindFirstFileA Lib "kernel32.dll" ( _
ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA) As LongPtr
Private Declare PtrSafe Function FindNextFileA Lib "kernel32.dll" ( _
ByVal hFindFile As LongPtr, _
ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare PtrSafe Function FindClose Lib "kernel32.dll" ( _
ByVal hFindFile As LongPtr) As Long
Private Declare PtrSafe Function FileTimeToLocalFileTime Lib "kernel32.dll" ( _
ByRef lpFileTime As FILETIME, _
ByRef lpLocalFileTime As FILETIME) As Long
Private Declare PtrSafe Function FileTimeToSystemTime Lib "kernel32.dll" ( _
ByRef lpFileTime As FILETIME, _
ByRef lpSystemTime As SYSTEMTIME) As Long
#Else
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" ( _
ByRef lpFileTime As FILETIME, _
ByRef lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32.dll" ( _
ByRef lpFileTime As FILETIME, _
ByRef lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FindClose Lib "kernel32.dll" ( _
ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFileA Lib "kernel32.dll" ( _
ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFileA Lib "kernel32.dll" ( _
ByVal hFindFile As Long, _
ByRef lpFindFileData As WIN32_FIND_DATA) As Long
#End If

#If Win64 Then
Private Const INVALID_HANDLE_VALUE As LongPtr = -1
#Else
Private Const INVALID_HANDLE_VALUE As Long = -1&
#End If

Private Sub FindFiles(ByVal pvstrFolderPath As String)
#If Win64 Then
Dim lngSearch As LongPtr
#Else
Dim lngSearch As Long
#End If
...

Private Sub GetFilesInFolder(ByVal pvstrFolderPath As String)
#If Win64 Then
Dim lngSearch As LongPtr
#Else
Dim lngSearch As Long
#End If
...
Gruß
Nepumuk
Anzeige
AW: 32 bit auf 64 bit umschreiben
05.12.2022 14:57:44
Dogan
Hallo Nepumuk,
ich bedanke mich recht herzlich...
AW: 32 bit auf 64 bit umschreiben
05.12.2022 11:24:56
Nepumuk
Hallo Dogan,
so:

Option Explicit
Private Declare PtrSafe Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" ( _
ByVal pszPath As String, _
ByVal dwFileAttributes As Long, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Long, _
ByVal uFlags As Long) As LongPtr
Private Const MAX_PATH As Long = 260
Private Type SHFILEINFO
hIcon As LongPtr
iIcon As Long
dwAttributes As Long
szDisplayName(0 To MAX_PATH - 1) As Byte
szTypeName(0 To 79) As Byte
End Type
Gruß
Nepumuk
Anzeige
;

Forumthreads zu verwandten Themen

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Infobox / Tutorial

32 Bit auf 64 Bit umschreiben in Excel VBA


Schritt-für-Schritt-Anleitung

Um Deine VBA-Anwendungen von 32 Bit auf 64 Bit umzustellen, folge diesen Schritten:

  1. Identifizierung der Deklarationen: Überprüfe alle Private Declare-Anweisungen in Deinem Code. Diese müssen angepasst werden, um die 64-Bit-Versionen zu unterstützen.

  2. Verwendung von #If VBA7 Then: Füge die Bedingung #If VBA7 Then ein, um sicherzustellen, dass der Code sowohl in 32-Bit- als auch in 64-Bit-Umgebungen funktioniert. Ein Beispiel für die Umstellung einer Funktion könnte so aussehen:

    #If VBA7 Then
    Private Declare PtrSafe Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As LongPtr
    #Else
    Private Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    #End If
  3. Anpassung von Datentypen: In 64-Bit-Versionen sollten Datentypen, die Handles repräsentieren, als LongPtr deklariert werden. Dies betrifft Funktionen wie FindFirstFile oder FindNextFile, die in kernel32.dll enthalten sind.

  4. Testen des Codes: Nach den Anpassungen teste Deinen Code gründlich, um sicherzustellen, dass keine Excel Fehler beim Kompilieren 64-bit auftreten.


Häufige Fehler und Lösungen

  • Fehler: „Kompilierungsfehler: Benutzerdefinierter Typ nicht definiert“

    • Lösung: Stelle sicher, dass alle verwendeten Datentypen wie WIN32_FIND_DATA korrekt definiert sind.
  • Fehler: „Kompilierungsfehler: Ungültiger Prozeduraufruf oder ungültiges Argument“

    • Lösung: Überprüfe die Parameter der aufgerufenen Funktionen, insbesondere die Übergabe von LongPtr anstelle von Long.
  • Fehler: „Kompilierungsfehler: Funktion nicht gefunden“

    • Lösung: Kontrolliere, ob die Deklarationen korrekt sind und die Bibliotheken vorhanden sind.

Alternative Methoden

  1. Verwendung von Excel-Add-Ins: Manchmal kann es sinnvoll sein, ein Add-In zu verwenden, das bereits für 64-Bit optimiert ist. Dies kann den Aufwand für die Umstellung reduzieren.

  2. VBA-Funktionen: Überprüfe, ob Du native VBA-Funktionen verwenden kannst, die bereits für 64-Bit optimiert sind, anstatt auf API-Funktionen zurückzugreifen.


Praktische Beispiele

Hier sind einige Beispiele, wie Du 32-Bit-Funktionen in 64-Bit umschreiben kannst:

  • Von 32 Bit nach 64 Bit:

    ' 32 Bit
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long
    
    ' 64 Bit
    #If VBA7 Then
    Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As LongPtr
    #Else
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long
    #End If
  • Beispiel für die Verwendung von kernel32.dll:

    #If Win64 Then
    Private Declare PtrSafe Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As LongPtr
    #Else
    Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
    #End If

Tipps für Profis

  • Stelle sicher, dass Deine Entwicklungsumgebung (IDE) auf die 64-Bit-Version von Excel eingestellt ist, um Konflikte zu vermeiden.
  • Nutze die PtrSafe-Anweisung bei allen Declare-Funktionen, die in 64-Bit verwendet werden.
  • Verwende die Bedingung #If Win64 für spezifische Anpassungen, die nur in einer 64-Bit-Umgebung benötigt werden.

FAQ: Häufige Fragen

1. Wie finde ich heraus, ob ich eine 32-Bit oder 64-Bit Version von Excel habe? Du kannst dies unter „Datei“ > „Konto“ > „Über Excel“ überprüfen. Dort wird die Version angezeigt.

2. Was passiert, wenn ich die 32-Bit-Funktionen nicht anpasse? Wenn Du die Funktionen nicht anpasst, können sie in der 64-Bit-Version von Excel nicht korrekt ausgeführt werden, was zu Fehlern führt.

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige