Results 1 to 14 of 14

Thread: Declare function no longer works

  1. #1

    Declare function no longer works

    I'm using Excel for Office 365 and up until a couple of weeks ago (or thereabouts) this statement gave no trouble:

    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
                (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Now, trying to save a program with this line in it gives me a sharing violation and an access violation. Can someone please help? Thanks!!
    Last edited by Aussiebear; 02-15-2022 at 06:50 PM. Reason: Added code tags to supplied code

  2. #2
    if you are using x64, you need to change the declaration, with conditional compiler directives:

    #If VBA7 Then
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
    (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
    #Else
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
    (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    #End If
    Last edited by Aussiebear; 02-15-2022 at 06:51 PM. Reason: Added code tags to supplied code

  3. #3
    That's what I had thought, but it still doesn't work. I get the following error message: "The document was saved successfully, but Excel cannot re-open it because of a sharing violation. Please close the document and try to open it again."

  4. #4
    Quote Originally Posted by arnelgp View Post
    if you are using x64, you need to change the declaration, with conditional compiler directives:

    #If VBA7 Then
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
    (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
    #Else
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
    (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    #End If

  5. #5

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,718
    Location
    According to the MS 32/64 document


    Declare PtrSafe Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Quote Originally Posted by Paul_Hossler View Post
    According to the MS 32/64 document


    Declare PtrSafe Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
    that is if you are using Office 2010 and newer.

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,120
    Location
    I believe the OP stated they were using Excel for Office 365
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    All handles should be LongPtr in a 64bit environment.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    In post 1, only OP is using 365.

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,718
    Location
    Quote Originally Posted by Bob Phillips View Post
    All handles should be LongPtr in a 64bit environment.

    Agreed, but I think it also works with 32 bit office

    https://docs.microsoft.com/en-us/off...6)%26rd%3Dtrue


    LongPtr (Long integer on 32-bit systems, LongLong integer on 64-bit systems) variables are stored as:

    • Signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647 on 32-bit systems
    • Signed 64-bit (8-byte) numbers ranging in value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit systems



    LongPtr is not a true data type because it transforms to a Long in 32-bit environments, or a LongLong in 64-bit environments. Using LongPtr enables writing portable code that can run in both 32-bit and 64-bit environments. Use LongPtr for pointers and handles.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    <p>
    Quote Originally Posted by Paul_Hossler View Post
    Agreed, but I think it also works with 32 bit office https://docs.microsoft.com/en-us/off...6)%26rd%3Dtrue
    That would suggest that we should just declare every long as LongPtr, 32bit or 64bit, and the conditional code is only necessary for PtrSafe? Might explain why it works when I have declared a non-handle variable as LongPtr.</p>
    Last edited by Paul_Hossler; 02-17-2022 at 11:13 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Quote Originally Posted by Bob Phillips View Post
    That would suggest that we should just declare every long as LongPtr, 23bit or 64bit, and the conditional code is only necessary for PtrSafe? Might explain why it works when I have declared a non-handle variable as LongPtr.
    The conditional compilation for VBA7 is necessary for LongPtr and PtrSafe because these keywords only exist in VBA7.

    As for "just declare every long as LongPtr", you will get away with declaring most Long variables as LongPtr but not all of them.
    If you are interested in the details, I recommend my text Windows API declarations in VBA for 64-bit. (It mentions Microsoft Access in some locations, but the same is also true Excel, Word, and any other VBA enabled application.)
    Learn VBA from the ground up with my VBA Online Courses.

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,718
    Location
    Quote Originally Posted by PhilS View Post
    The conditional compilation for VBA7 is necessary for LongPtr and PtrSafe because these keywords only exist in VBA7. As for "just declare every long as LongPtr", you will get away with declaring most Long variables as LongPtr but not all of them. If you are interested in the details, ...

    Don't know


    I don't have enough versions (2010, 2013, 2016, 365) and 32/64 bit to test
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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