+ Reply to Thread
Results 1 to 9 of 9

Type Mismatch error when run on a 64 bit machine

  1. #1
    Registered User
    Join Date
    07-26-2017
    Location
    St. Louis, MO
    MS-Off Ver
    2010
    Posts
    8

    Type Mismatch error when run on a 64 bit machine

    I inherited a macro embedded in a chart of accounts. It works great when used on a 32 bit machine. 64 bit machines are slowly being rolled out and a user is having trouble with the macro on a 64 bit machine. The users presses a search button to display a pop-up window. They enter the account number and press the "find" button and it takes them to the first instance of what they entered. If they push "find" again, it takes them to the 2nd instance and so on.

    I know I had to enter "ptrsafe" in each declaration and I have already done that. However, we are now getting a type mismatch error on the MsgBoxEx function. The "AddressOf zWindowProc" is highlighted in this function.

    Can anyone help with what needs to be changed? Thanks for your help.........

    Option Explicit

    Public Enum ePosMsgBox
    eTopLeft
    eTopRight
    eTopCentre
    eBottomLeft
    eBottomRight
    eBottomCentre
    eCentreScreen
    eCentreDialog
    End Enum

    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal zlhHook As Long) As Long

    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

    Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long

    Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

    Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

    Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

    Private Const GWL_HINSTANCE = (-6)
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_NOZORDER = &H4
    Private Const SWP_NOACTIVATE = &H10
    Private Const HCBT_ACTIVATE = 5
    Private Const WH_CBT = 5

    Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As Long

    Private Declare PtrSafe Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long

    Private zlhHook As Long
    Private zePosition As ePosMsgBox

    Function MsgboxEx(Prompt As String, Optional Buttons As VbMsgBoxStyle, Optional Title, Optional HelpFile, Optional Context, Optional Position As ePosMsgBox) As VbMsgBoxResult
    Dim lhInst As Long
    Dim lThread As Long

    lhInst = GetWindowLong(GetForegroundWindow, GWL_HINSTANCE)
    lThread = GetCurrentThreadId()
    zlhHook = SetWindowsHookEx(WH_CBT, AddressOf zWindowProc, lhInst, lThread)

    zePosition = Position

    MsgboxEx = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
    End Function

    Private Function zWindowProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim tFormPos As RECT, tMsgBoxPos As RECT, tScreenWorkArea As RECT
    Dim lLeft As Long, lTop As Long
    Static sbRecursive As Boolean

    If lMsg = HCBT_ACTIVATE Then
    On Error Resume Next
    tScreenWorkArea = ScreenWorkArea
    GetWindowRect GetForegroundWindow, tFormPos
    GetWindowRect wParam, tMsgBoxPos

    Select Case zePosition
    Case eCentreDialog
    lLeft = (tFormPos.Left + (tFormPos.Right - tFormPos.Left) / 2) - ((tMsgBoxPos.Right - tMsgBoxPos.Left) / 2)
    lTop = (tFormPos.Top + (tFormPos.Bottom - tFormPos.Top) / 2) - ((tMsgBoxPos.Bottom - tMsgBoxPos.Top) / 2)

    Case eCentreScreen
    lLeft = ((tScreenWorkArea.Right - tScreenWorkArea.Left) - (tMsgBoxPos.Right - tMsgBoxPos.Left)) / 2
    lTop = ((tScreenWorkArea.Bottom - tScreenWorkArea.Top) - (tMsgBoxPos.Bottom - tMsgBoxPos.Top)) / 2

    Case eTopLeft
    lLeft = tScreenWorkArea.Left
    lTop = tScreenWorkArea.Top

    Case eTopRight
    lLeft = tScreenWorkArea.Right - (tMsgBoxPos.Right - tMsgBoxPos.Left)
    lTop = tScreenWorkArea.Top

    Case eTopCentre
    lLeft = ((tScreenWorkArea.Right - tScreenWorkArea.Left) - (tMsgBoxPos.Right - tMsgBoxPos.Left)) / 2
    lTop = tScreenWorkArea.Top


    Case eBottomLeft
    lLeft = tScreenWorkArea.Left
    lTop = tScreenWorkArea.Bottom - (tMsgBoxPos.Bottom - tMsgBoxPos.Top)

    Case eBottomRight
    lLeft = tScreenWorkArea.Right - (tMsgBoxPos.Right - tMsgBoxPos.Left)
    lTop = tScreenWorkArea.Bottom - (tMsgBoxPos.Bottom - tMsgBoxPos.Top)

    Case eBottomCentre
    lLeft = ((tScreenWorkArea.Right - tScreenWorkArea.Left) - (tMsgBoxPos.Right - tMsgBoxPos.Left)) / 2
    lTop = tScreenWorkArea.Bottom - (tMsgBoxPos.Bottom - tMsgBoxPos.Top)

    End Select

    If lLeft < 0 And sbRecursive = False Then
    sbRecursive = True
    zePosition = eCentreScreen
    zWindowProc HCBT_ACTIVATE, wParam, lParam
    sbRecursive = False
    Exit Function
    End If

    SetWindowPos wParam, 0, lLeft, lTop, 10, 10, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE

    UnhookWindowsHookEx zlhHook
    End If
    zWindowProc = False

    End Function

    Function ScreenWorkArea() As RECT
    Dim tScreen As RECT
    Dim lRet As Long
    Const SPI_GETWORKAREA = 48

    lRet = SystemParametersInfo(SPI_GETWORKAREA, vbNull, tScreen, 0)
    ScreenWorkArea = tScreen
    End Function

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Type Mismatch error when run on a 64 bit machine

    Removed post -
    OP added code tags to the post below.
    Thank you
    Last edited by gmr4evr1; 08-01-2017 at 11:05 AM. Reason: Removed post
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    07-26-2017
    Location
    St. Louis, MO
    MS-Off Ver
    2010
    Posts
    8

    Re: Type Mismatch error when run on a 64 bit machine

    I apologize for not complying. I tried to edit and was unable to. so I copied / pasted the code blow and added the [CODE} {/CODE]Please Login or Register to view this content.[/CODE]

  4. #4
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Type Mismatch error when run on a 64 bit machine

    Clicking the Edit button at the bottom of your original post wouldn't allow you to edit it?

  5. #5
    Registered User
    Join Date
    07-26-2017
    Location
    St. Louis, MO
    MS-Off Ver
    2010
    Posts
    8

    Re: Type Mismatch error when run on a 64 bit machine

    It appeared that the post opened up to edit it but there was nothing there; which is why I re-posted it in the reply

  6. #6
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Type Mismatch error when run on a 64 bit machine

    OK. Thank you for adding the code tags.

  7. #7
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Type Mismatch error when run on a 64 bit machine

    Hi @shaves. Let me start by saying I probably know less than you do about using API calls. However, I've noticed you don't seem to make use of the LongPtr data type when handling memory addresses (hwnd), nor do you employ conditional compilation. These topics are discussed at:
    http://www.jkp-ads.com/articles/apideclarations.asp. Scroll down past the API Declaration List, from whence comes the following code example:
    Please Login or Register  to view this content.
    Hope this helps-Lee

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 08-01-2017 at 12:30 PM.

  8. #8
    Registered User
    Join Date
    07-26-2017
    Location
    St. Louis, MO
    MS-Off Ver
    2010
    Posts
    8

    Re: Type Mismatch error when run on a 64 bit machine

    @leelnich.............thanks. I came across that link while searching. How do you know which ones use LongPtr and which ones still use long? Thanks.........

  9. #9
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Type Mismatch error when run on a 64 bit machine

    From Post#7 link:
    Which Longs should become LongPtr?

    It's actually pretty easy to determine what requires LongPtr and what can stay as Long. The only things that require LongPtr are function arguments or return values that represent addresses in memory. This is because a 64-bit OS has a memory space that is too large to hold in a Long data type variable. Arguments or return values that represent data will still be declared Long even in 64-bit.

    The SendMessage API is a good example because it uses both types:

    32-bit:
    Public Declare Function SendMessageA Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long

    64 bit:
    Public Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hWnd As LongPtr, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As LongPtr

    The first argument -hWnd- is a window handle, which is an address in memory. The return value is a pointer to a function, which is also an address in memory. Both of these must be declared LongPtr in 64-bit VBA. The arguments wMsg and wParam are used to pass data, so they can be Long in both 32-bit and 64-bit.

    How to determine what is a memory address and what is data? You just have to read the MSDN documentation for the API functions (the C++ version) and it will tell you. Anything called a handle, pointer, brush or any other object type will require a LongPtr in 64-bit. Anything that is strictly data can stay as Long.
    MS offers a (download) list of 64 bit declarations @ https://www.microsoft.com/en-us/down...s.aspx?id=9970

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 08-01-2017 at 12:51 PM.

+ 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. [SOLVED] RunTime Error 13 ( type mismatch ) error is comming TextboxAfter_Update
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2017, 03:55 AM
  2. Type mismatch error 13
    By VBANewbie_! in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2014, 01:06 PM
  3. Run Type error 13 Type Mismatch
    By Affan Khan in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-13-2012, 12:58 PM
  4. [SOLVED] Run-type error 13 type mismatch
    By misop in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-05-2012, 04:08 AM
  5. Complile Error: Type Mismatch ??? After adding error trap
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2012, 03:50 PM
  6. Conditional Formatting - Run Time Error '13' Type Mismatch Error
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2011, 07:37 PM
  7. [SOLVED] Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM

Tags for this Thread

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