+ Reply to Thread
Results 1 to 3 of 3

Convert 32 bit VBA code to 64 bit

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2018
    Location
    [email protected]
    MS-Off Ver
    Office 2013
    Posts
    1

    Convert 32 bit VBA code to 64 bit

    hi Everyone!

    Thanks in advance for the help.

    I've been using the below code on a 32 bit excel but excel was recently updated to 64 bit and its not working anymore. not sure how I can update the code to make this work now.

    Please help.

    Option Explicit
    
    Private Declare Function OpenClipboard Lib "user32.dll" ( _
        ByVal hwnd As Long) As Long
    Private Declare Function CloseClipboard Lib "user32.dll" () As Long
    Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
    Private Declare Function SetClipboardData Lib "user32.dll" ( _
        ByVal wFormat As Long, _
        ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32.dll" ( _
        ByVal wFlags As Long, _
        ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32.dll" ( _
        ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32.dll" ( _
        ByVal hMem As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32.dll" ( _
        ByVal hMem As Long) As Long
    Private Declare Function lstrcpy Lib "kernel32.dll" ( _
        ByVal lpStr1 As Any, _
        ByVal lpStr2 As Any) As Long
    
    Private Const CF_TEXT As Long = 1&
    
    Private Const GMEM_MOVEABLE As Long = 2
    
    Sub CopyContent()
        Call StringToClipboard(ActiveCell.Value)
    End Sub
    
    Private Sub StringToClipboard(strText As String)
        Dim lngIdentifier As Long, lngPointer As Long
        lngIdentifier = GlobalAlloc(GMEM_MOVEABLE, Len(strText) + 1)
        lngPointer = GlobalLock(lngIdentifier)
        Call lstrcpy(ByVal lngPointer, strText)
        Call GlobalUnlock(lngIdentifier)
        Call OpenClipboard(0&)
        Call EmptyClipboard
        Call SetClipboardData(CF_TEXT, lngIdentifier)
        Call CloseClipboard
        Call GlobalFree(lngIdentifier)
    End Sub

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Convert 32 bit VBA code to 64 bit

    Welcome to the forum!

    'https://stackoverflow.com/questions/35416662/text-to-clipboard-in-vba-windows-10-issue
    Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hWnd As LongPtr) As LongPtr
    Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As LongPtr
    Private Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
    Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
    Private Declare PtrSafe Function GetClipboardData Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
    Private Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As LongPtr, ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongPtr
    Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As Long
    Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
    
    Const GMEM_MOVEABLE As Long = &H2
    Const GMEM_ZEROINIT As Long = &H40
    Const CF_UNICODETEXT As Long = &HD
    
    Sub SetClipboard(sUniText As String)
        Dim iStrPtr As Long
        Dim iLock As Long
        Dim iLen As Long
    
        OpenClipboard 0&
        EmptyClipboard
        iLen = LenB(sUniText) + 2&
        iStrPtr = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, iLen)
        iLock = GlobalLock(iStrPtr)
        lstrcpy iLock, StrPtr(sUniText)
        GlobalUnlock iStrPtr
        SetClipboardData CF_UNICODETEXT, iStrPtr
        CloseClipboard
    End Sub
    
    Function GetClipboard() As String
        Dim iStrPtr As LongPtr
        Dim iLock As LongPtr
        Dim iLen As Long
        Dim sUniText As String
    
        Const CF_UNICODETEXT As Long = 13&
    
        OpenClipboard 0&
    
        If IsClipboardFormatAvailable(CF_UNICODETEXT) Then
            iStrPtr = GetClipboardData(CF_UNICODETEXT)
            If iStrPtr Then
                iLock = GlobalLock(iStrPtr)
                iLen = GlobalSize(iStrPtr)
                sUniText = String$(iLen \ 2& - 1&, vbNullChar)
                lstrcpy StrPtr(sUniText), iLock
                GlobalUnlock iStrPtr
            End If
            GetClipboard = sUniText
        End If
    
        CloseClipboard
    End Function
    
    Sub CopyContent()
        SetClipboard ActiveCell
    End Sub

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Convert 32 bit VBA code to 64 bit

    Hello ElHa$on,

    This version will work on all platforms of Windows and Office.

    Option Explicit
    
    Private Const CF_TEXT       As Long = 1
    Private Const GMEM_MOVEABLE As Long = 2
    
    #If VBA7 Then
        ' // Code is running in VBA version 7.
        #If Win64 Then
            ' // Running in Office 64 bit version.
            Private Declare PtrSafe Function OpenClipboard Lib "user32.dll" (ByVal hwnd As LongPtr) As Long
            Private Declare PtrSafe Function CloseClipboard Lib "user32.dll" () As Long
            Private Declare PtrSafe Function EmptyClipboard Lib "user32.dll" () As Long
            Private Declare PtrSafe Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
            Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
            Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
            Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As LongPtr) As Long
            Private Declare PtrSafe Function GlobalFree Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
            Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" (ByVal lpStr1 As Any, ByVal lpStr2 As Any) As Long
            Private lngIdentifier As LongPtr
            Private lngPointer As LongPtr
        #Else
            ' // Running in Office 32 bit version.
            Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hwnd As Long) As Long
            Private Declare Function CloseClipboard Lib "user32.dll" () As Long
            Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
            Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
            Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
            Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
            Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
            Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
            Private Declare Function lstrcpy Lib "kernel32.dll" (ByVal lpStr1 As Any, ByVal lpStr2 As Any) As Long
            Private lngIdentifier As Long
            Private lngPointer As Long
        #End If
    #Else
        ' // Code is running in VBA version 6 or earlier.
        Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hwnd As Long) As Long
        Private Declare Function CloseClipboard Lib "user32.dll" () As Long
        Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
        Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
        Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
        Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
        Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
        Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
        Private Declare Function lstrcpy Lib "kernel32.dll" (ByVal lpStr1 As Any, ByVal lpStr2 As Any) As Long
        Private lngIdentifier As Long
        Private lngPointer As Long
    #End If
    
    Sub CopyContent()
        Call StringToClipboard(ActiveCell.Value)
    End Sub
    
    Private Sub StringToClipboard(strText As String)
        lngIdentifier = GlobalAlloc(GMEM_MOVEABLE, Len(strText) + 1)
        lngPointer = GlobalLock(lngIdentifier)
        Call lstrcpy(ByVal lngPointer, strText)
        Call GlobalUnlock(lngIdentifier)
        Call OpenClipboard(0&)
        Call EmptyClipboard
        Call SetClipboardData(CF_TEXT, lngIdentifier)
        Call CloseClipboard
        Call GlobalFree(lngIdentifier)
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Convert InternetExplorer Code to XML Code
    By richardking in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2019, 12:04 AM
  2. [SOLVED] Simple VBA code to convert text to number (simplify/fix current code)
    By kenenthpaul0401 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2018, 10:47 AM
  3. Convert Excel Code to Access Code
    By Ruseju in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-17-2016, 02:36 AM
  4. VBA code to convert all data + formatting of entire sheet to VBA code
    By jocanon in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-14-2016, 06:56 PM
  5. Convert Cell Formula to Convert Text to VBA Code
    By PY_ in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-08-2014, 05:51 PM
  6. How To Convert VBA Code To DLL/XLL?
    By CaliKidd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-17-2011, 11:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1