+ Reply to Thread
Results 1 to 15 of 15

Thread: An upgrade to 64bit Office 2010 creates a function error in a standard module?

  1. #1
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    An upgrade to 64bit Office 2010 creates a function error in a standard module?

    An upgrade was done on the first of many computers in our ogranization; an 'upgrade' to a 64bit operating system, which caused the following error in one of my modules:

    "Complile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with PtrSafe attribute."

    The code below is what the error applied to. I am thinking I have to change something on the .dll? Anyway, I am going to need the code to work on both 32bit and 64bit operating systems for some time. Any help is greatly appreciated!

    
    
    'Constants
     Const IMAGE_ICON As Long = &H1
     Const WM_SETICON As Long = &H80
     Const ICON_BIG As Long = &H1
    
    'Constants for Load Image's fuLoad Parameter (Load Resource)
     Const LR_DEFAULTCOLOR As Long = &H0
     Const LR_MONOCHROME As Long = &H1
     Const LR_COLOR As Long = &H2
     Const LR_COPYRETURNORG As Long = &H4
     Const LR_COPYDELETEORG As Long = &H8
     Const LR_LOADFROMFILE As Long = &H10
     Const LR_LOADTRANSPARENT As Long = &H20
     Const LR_DEFAULTSIZE As Long = &H40
     Const LR_VGACOLOR As Long = &H80
     Const LR_LOADMAP3DCOLORS As Long = &H1000
     Const LR_CREATEDIBSECTION As Long = &H2000
     Const LR_COPYFROMRESOURCE As Long = &H4000
     Const LR_SHARED As Long = &H8000
    
    'Returns an Icon from a File (.ico)
     Private Declare Function LoadImage _
       Lib "user32.dll" Alias "LoadImageA" _
         (ByVal hInst As Long, _
          ByVal lpsz As String, _
          ByVal uType As Long, _
          ByVal cxDesired As Long, _
          ByVal cyDesired As Long, _
          ByVal fuLoad As Long) _
       As Long
    
    'Direct System what to do with the Window
     Private Declare Function SendMessage _
       Lib "user32.dll" Alias "SendMessageA" _
         (ByVal hwnd As Long, _
          ByVal wMsg As Long, _
          ByVal wParam As Long, _
          lParam As Long) _
       As Long
    
    'Redraw the Icons on the Window's Title Bar
     Private Declare Function DrawMenuBar _
       Lib "user32.dll" _
         (ByVal hwnd As Long) _
       As Long
    
    
    Public Function ChangeExcelIcon(ByVal Icon_File_Path As String)
    
      Dim hwnd As Long
      Dim hIcon As Long
      Dim LoadMask As Long
    
        hwnd = Application.hwnd
        LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED
        hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask)
    
        Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon)
        Call DrawMenuBar(hwnd)
    
    End Function
    Last edited by jonvanwyk; 01-05-2012 at 11:34 AM. Reason: Clarity

  2. #2
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64 bit operating system creates a function error in a standard modu

    That error is not related to a 64 bit operating system, but to using 64bit Office. I would suggest you read through this MSDN article. There is also a download available that lists many of the API declaration statements in both 32 and 64 bit syntax.
    Good luck.

  3. #3
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64 bit operating system creates a function error in a standard modu

    @onerrorgoto0: That is a good start at least to know that it is the version of Office and not the OS. My programming skills may not be sufficient to know on my own how to modify the delcaration statements to be compatible with both. So again, thank you to any one that can assist with this. I am sure it is probably something simple, I just have never done it before. I pride myself on just never having to ask the same question twice. It is how I learn

  4. #4
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    I have been working on following through with OnErrorGoto0's suggestions, but I have insufficient administrative rights to run some of the stuff required to do it. any assistance offered with achieving resolution to this issue is greatly appreciated!

  5. #5
    Cheeky Forum Moderator Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2003, 2007, 2010
    Posts
    3,712

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    In 64-bit Office VBA, you'll need to declare API calls this way:

     Private Declare PtrSafe Function LoadImage _
       Lib "user32.dll" Alias "LoadImageA" _
         (ByVal hInst As Long, _
    ...etc


    Does that help?
    Regards,

    Ron
    Microsoft MVP - Excel
    (Oct 2006 - Sep 2012)

    Click here to see the Forum Rules

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    You will also need to replace any pointer arguments so that they are LongPtr rather than Long. I will see if I can put it together for you shortly (if the forum remains up) but I do not have 64bit Excel to test on, I am afraid, so I will have to leave that to you.
    Good luck.

  7. #7
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    OK, based on the Microsoft documentation, I think the code should be:
    'Constants
     Const IMAGE_ICON As Long = &H1
     Const WM_SETICON As Long = &H80
     Const ICON_BIG As Long = &H1
    
    'Constants for Load Image's fuLoad Parameter (Load Resource)
     Const LR_DEFAULTCOLOR As Long = &H0
     Const LR_MONOCHROME As Long = &H1
     Const LR_COLOR As Long = &H2
     Const LR_COPYRETURNORG As Long = &H4
     Const LR_COPYDELETEORG As Long = &H8
     Const LR_LOADFROMFILE As Long = &H10
     Const LR_LOADTRANSPARENT As Long = &H20
     Const LR_DEFAULTSIZE As Long = &H40
     Const LR_VGACOLOR As Long = &H80
     Const LR_LOADMAP3DCOLORS As Long = &H1000
     Const LR_CREATEDIBSECTION As Long = &H2000
     Const LR_COPYFROMRESOURCE As Long = &H4000
     Const LR_SHARED As Long = &H8000
    
    'Returns an Icon from a File (.ico)
     Private Declare Function LoadImage _
       Lib "user32.dll" Alias "LoadImageA" _
         (ByVal hInst As LongPtr, _
          ByVal lpsz As String, _
          ByVal uType As Long, _
          ByVal cxDesired As Long, _
          ByVal cyDesired As Long, _
          ByVal fuLoad As Long) _
       As LongPtr
    
    'Direct System what to do with the Window
     Private Declare Function SendMessage _
       Lib "user32.dll" Alias "SendMessageA" _
         (ByVal hwnd As LongPtr, _
          ByVal wMsg As Long, _
          ByVal wParam As LongPtr, _
          lParam As Long) _
       As LongPtr
    
    'Redraw the Icons on the Window's Title Bar
     Private Declare PtrSafe Function DrawMenuBar _
       Lib "user32.dll" _
         (ByVal hwnd As LongPtr) _
       As Long
    
    
    Public Function ChangeExcelIcon(ByVal Icon_File_Path As String)
    
      Dim hwnd As LongPtr
      Dim hIcon As LongPtr
      Dim LoadMask As Long
    
        hwnd = Application.hwnd
        LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED
        hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask)
    
        Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon)
        Call DrawMenuBar(hwnd)
    
    End Function
    Can you let me know if that works OK?
    Good luck.

  8. #8
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    If it should run on both 64 bit and older version you have to declare in an if statement.

    #If Win64
       ' When declaring for 64 bit then you should do as ron said with ptrsafe
    #Else
       ' as you've done it so far
    #End if
    When opening in 64 bit the else part will be marked with red and the other way round if it's not 64 bit.
    This is how it's supposed to be.
    Please take time to read the forum rules

  9. #9
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    Strictly speaking, as in the MS article I linked to, you only need #Win64 if you are using 64bit specific code - e.g. code that uses LongLong. For the rest you only need the #VBA7 constant, since 32bit Office 2010 is fine with LongPtr and PtrSafe.
    Good luck.

  10. #10
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    @onErrorGoto0: Thank you very much for taking such time to assist me. I also do not have 64bit Office, so will have to wait to test your code on the computer that has it when it becomes available. I will keep you posted as I know more...and thank you again for your help!

  11. #11
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    PtrSafe Compile Error.jpg

    I installed your code into module 3 of my workbook (where it was originally located before modification). Before I could text it on the 64bit Office, it gave me the attached Complile Error on my 32bit version. Thoughts?

  12. #12
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    Per the article I linked to, if you do not have office 2010 you will need conditional compilation since VBA6 has no concept of PtrSafe or any 64bit compatibility issues.
    Good luck.

  13. #13
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    I am sorry. I have attempted multiple ways of accomplishing this, but am still at a loss. :-/

  14. #14
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    Try this

    Option Explicit
    
    'Constants
     Const IMAGE_ICON As Long = &H1
     Const WM_SETICON As Long = &H80
     Const ICON_BIG As Long = &H1
    
    'Constants for Load Image's fuLoad Parameter (Load Resource)
     Const LR_DEFAULTCOLOR As Long = &H0
     Const LR_MONOCHROME As Long = &H1
     Const LR_COLOR As Long = &H2
     Const LR_COPYRETURNORG As Long = &H4
     Const LR_COPYDELETEORG As Long = &H8
     Const LR_LOADFROMFILE As Long = &H10
     Const LR_LOADTRANSPARENT As Long = &H20
     Const LR_DEFAULTSIZE As Long = &H40
     Const LR_VGACOLOR As Long = &H80
     Const LR_LOADMAP3DCOLORS As Long = &H1000
     Const LR_CREATEDIBSECTION As Long = &H2000
     Const LR_COPYFROMRESOURCE As Long = &H4000
     Const LR_SHARED As Long = &H8000
    
    #If vba7 Then
       '############################################
       ' USE 64 BIT COMPATIBLE DECLARATIONS
       '############################################
       
       'Returns an Icon from a File (.ico)
        Private Declare ptrsafe Function LoadImage _
          Lib "user32.dll" Alias "LoadImageA" _
            (ByVal hInst As LongPtr, _
             ByVal lpsz As String, _
             ByVal uType As Long, _
             ByVal cxDesired As Long, _
             ByVal cyDesired As Long, _
             ByVal fuLoad As Long) _
          As LongPtr
       
       'Direct System what to do with the Window
        Private Declare ptrsafe Function SendMessage _
          Lib "user32.dll" Alias "SendMessageA" _
            (ByVal hwnd As LongPtr, _
             ByVal wMsg As Long, _
             ByVal wParam As LongPtr, _
             lParam As Long) _
          As LongPtr
       
       'Redraw the Icons on the Window's Title Bar
        Private Declare PtrSafe Function DrawMenuBar Lib "user32.dll" _
            (ByVal hwnd As LongPtr) As Long
    
    #Else
       '############################################
       ' USE 32 BIT COMPATIBLE DECLARATIONS
       '############################################
    
       'Returns an Icon from a File (.ico)
        Private Declare Function LoadImage _
          Lib "user32.dll" Alias "LoadImageA" _
            (ByVal hInst As Long, _
             ByVal lpsz As String, _
             ByVal uType As Long, _
             ByVal cxDesired As Long, _
             ByVal cyDesired As Long, _
             ByVal fuLoad As Long) _
          As Long
       
       'Direct System what to do with the Window
        Private Declare Function SendMessage _
          Lib "user32.dll" Alias "SendMessageA" _
            (ByVal hwnd As Long, _
             ByVal wMsg As Long, _
             ByVal wParam As Long, _
             lParam As Long) _
          As Long
       
       'Redraw the Icons on the Window's Title Bar
        Private Declare Function DrawMenuBar _
          Lib "user32.dll" _
            (ByVal hwnd As Long) _
          As Long
    #End If
    
    
    Public Function ChangeExcelIcon(ByVal Icon_File_Path As String)
    #If vba7 Then
      Dim hwnd As LongPtr
      Dim hIcon As LongPtr
    #Else
      Dim hwnd As Long
      Dim hIcon As Long
    #End If
      Dim LoadMask As Long
    
        hwnd = Application.hwnd
        LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED
        hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask)
    
        Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon)
        Call DrawMenuBar(hwnd)
    
    End Function
    Good luck.

  15. #15
    Valued Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2007
    Posts
    388

    Re: An upgrade to 64bit Office 2010 creates a function error in a standard module?

    I will see if it works. Thanks again for helping me out!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0