2

I am receiving a 'Type Mistmatch' error on the HWNDSrc = objIE.HWND line below but HWND is a Long property & HWNDSrc has been set as Long

Sub Test()
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
Dim HWNDSrc As Long
HWNDSrc = objIE.HWND
SetForegroundWindow HWNDSrc
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Mturks83
  • 89
  • 2
  • 9
  • Works fine for me... of course I had to remove the `SetForegroundWindow` line tho, because you did not include its definition. Note: You probably will need to add this too: `objIE.Visible = True` if you are expecting to be able to set it to the foreground. – braX Jan 06 '20 at 02:35
  • That's very strange - any idea why this would drive an error for me? Is it a setting I need to adjust? – Mturks83 Jan 06 '20 at 02:46
  • Looks like the guy below has the answer. I am using Win 10 64 bit OS, and 64 bit Office. – braX Jan 06 '20 at 02:54
  • I have removed the SetForegroundWindow code but still receiving the same error. If we are both running the exact same code should it not work for both of us? Thanks for your help – Mturks83 Jan 06 '20 at 02:55
  • Are you running 64 bit office and 64 bit windows 10? – braX Jan 06 '20 at 02:55

1 Answers1

2

SetForegroundWindow is a Windows API function that takes and returns LongPtr on 64-bit Office, and Long on 32 bit Office, so you need to account for that using conditional compilation constants. See this for reference.

Option Explicit

#If VBA7 Then
    Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As LongPtr

#Else
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#End If


Sub Test()

    Dim objIE As InternetExplorer
    Set objIE = New InternetExplorer

#If VBA7 Then
    Dim HWNDSrc As LongPtr
#Else
    Dim HWNDSrc As Long
#End If

    HWNDSrc = objIE.hwnd

    SetForegroundWindow HWNDSrc

End Sub
rickmanalexander
  • 599
  • 1
  • 6
  • 17