Adapt code to be compatible with either 64 bit or 32 bit Excel

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
A few years ago I switched to 64 bit Office. I am now revisiting some code that was written with a 32 bit version. I received an error re: compatibility.
I found that I can resolve this issue for my 64 bit code by converting the declaration to 'PtrSafe' as follows:
VBA Code:
#If VBA7 Then
Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, _
ByVal dwReserved As LongPtr) As LongPtr
This works fine. However, I have read that I should be able to add:
VBA Code:
#Else
  ...32 bit Declaration...
#End If
This supposedly should direct the compiler to select the correct declaration and proceed. However, the 32 bit declaration is picked up at compile and it will not run. Any insights?
Thank you,
David
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
If you are in 64, it will send you a compilation error and the declaration for 32 will be in red, but don't worry, the correct one will be executed, depending on the computer where you execute it.

VBA Code:
#If VBA7 Then
  Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, _
    ByVal dwReserved As LongPtr) As LongPtr
#Else
  Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, _
    ByVal dwReserved As LongPtr) As LongPtr
#End If
 
Upvote 0
Solution
You are correct; I was simply being scared off by the red lettering when in fact the code runs fine in spite of it.
Many thanks Dante!!
 
Upvote 1
One point about this:
I assume that this should actually look like this:
VBA Code:
#If VBA7 Then
  Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, _
    ByVal dwReserved As LongPtr) As LongPtr
#Else
  Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, _
    ByVal dwReserved As Long) As Long
#End If
ie: in the 32 bit declaration the variable 'Long' is used, rather than 'LongPtr' as in the 64 bit
 
Upvote 0
in the 32 bit declaration the variable 'Long' is used, rather than 'LongPtr' as in the 64 bit



The actual data type that LongPtr resolves to depends on the version of Office that it is running in: LongPtr resolves to Long in 32-bit versions of Office, and LongPtr resolves to LongLong in 64-bit versions of Office.

So no need to set to Long ;)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top