Am having issues with a get window function, it isn't finding the window and just ends up taking which ever window is in view.

I need it to find the actual window with the IE caption, take a print screen of it, close the window then paste into Excel.

The code works when you play from VBA but when attached to a button it fails to act correctly as mentioned above.

I have tried with the button to have the whole code behind it, and also where it does a call against the code which is listed below. - it has two get IE captions because when the URL that is used loads it has one name and launches a smartcard application which is why escape is used and then the IE caption changes.

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

    Private Const VK_SNAPSHOT As Byte = 44

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _
    lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal _
    nCmdShow As Long) As Long
    Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function EmptyClipboard Lib "user32" () As Long
    Public Declare Function CloseClipboard Lib "user32" () As Long

    Private Const SW_SHOWMAXIMIZED = 3
    Private Const VK_LCONTROL As Long = &HA2
    Private Const VK_V = &H56
    Private Const vk_Escape = 27
    Private Const KEYEVENTF_KEYUP = &H2

    Public Function ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
    End Function

    Sub Screenshot()

    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
    
    Dim IE As Object
    Dim hwnd As Long, IECaption As String
    Dim fdate As Date

    
    '~~> Change Status and unmerge
    Range("B11").Value = "Running"
    ThisWorkbook.Sheets("SAT").Range("B18:D33").UnMerge
    Application.WindowState = xlMinimized
    '~~> Launch URL
    
    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True

    IE.Navigate "URL LINK"

    Sleep 5000

    '~~> Get the caption of IE
    IECaption = "Clinical Console © - Internet Explorer"
    
    Sleep 3000
    
    '~~> Get handle of IE
    hwnd = FindWindow(vbNullString, IECaption)

    If hwnd = 0 Then
        MsgBox "IE Window Not found!"
        Exit Sub
    Else
        '~~> Maximize IE
         ShowWindow hwnd, SW_SHOWMAXIMIZED
    End If

    DoEvents
    '~~> Snapshot of window & Paste into Sheet location
    
    Sleep 5000
    
    keybd_event vk_Escape, 0, 0, 0
    
    Sleep 4000
    
    Call GetWindow
    
    Application.SendKeys "(%{1068})"
    
    Sleep 2000
    
    Application.SendKeys ("%{F4}")

    ThisWorkbook.Sheets("SAT").Range("B18").Select
    ActiveSheet.Paste Destination:=Worksheets("SAT").Range("B18")
    Application.WindowState = xlMaximized

    '~~> Tidy up and update status
    Sheets("SAT").Range("B18:D33").Merge
    Range("B11").Value = "Completed: Screen Captured"
    'ActiveWorkbook.Save

    End Sub

    Sub CallScreenshot()

    Call Screenshot

    End Sub

    Sub GetWindow()
    Dim IE As Object
    Dim hwnd As Long, IECaption As String
    Dim fdate As Date

    '~~> Get the caption of IE
    IECaption = "Clinical Console © Login Error - Internet Explorer"
 
    '~~> Get handle of IE
    hwnd = FindWindow(vbNullString, IECaption)

    If hwnd = 0 Then
        MsgBox "IE Window Not found!"
        Exit Sub
    Else
        '~~> Maximize IE
         ShowWindow hwnd, SW_SHOWMAXIMIZED
    End If
    End Sub