Notifications
Clear all

Manipulação de Janelas do Windows com VBA

1 Posts
1 Usuários
1 Reactions
2,004 Visualizações
(@raygsson)
Posts: 0
New Member
Topic starter
 

Venho compartilhar com vocês um conhecimento precioso.

No código abaixo estou usando algumas APIs do Windows para manipular as janelas/avisos abertos.

Sabe aquela tela de confirmação perguntando Sim ou Não que você precisa clicar com VBA? ou ainda OK/Salvar/Cancelar/etc, o que precisar de click ou apenas fechar.

Só quem já fez alguma automação e travou nessas telas sabe como é importante saber mexer com isso 🤣

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function FindWindowExW Lib "user32" (ByVal hWndParent As LongPtr, Optional ByVal hwndChildAfter As LongPtr, Optional ByVal lpszClass As LongPtr, Optional ByVal lpszWindow As LongPtr) As LongPtr

Public Function IdBotão(JanelaMãe, Texto)
Dim TextoAPI As String * 255
ElementoFilho = FindWindowExW(JanelaMãe)
Do While ElementoFilho <> 0
    TextoElemento = Left$(TextoAPI, GetWindowText(ElementoFilho, ByVal TextoAPI, 255))
    If TextoElemento Like "*" & Texto & "*" Then
       IdBotão = ElementoFilho
       Exit Function
    End If
  ElementoFilho = FindWindowExW(JanelaMãe, ElementoFilho)
Loop
End Function

Sub ManipulandoJanelas()

'COMANDOS BÁSICOS
CLICAR = "&HF5"
FECHAR = "&H10"

'LOCALIZA A JANELA PELO TITULO
Janela = FindWindow(vbNullString, "Excluir Arquivo")

'PODERIA FECHAR A JANELA COM ESSE COMANDO:
'SendMessage Janela, FECHAR, 0&, 0&

'LOCALIZA BOTÃO PELO TEXTO
Botão = IdBotão(Janela, "Sim")

'ENVIA O COMANDO P/ O BOTÃO
SendMessage Botão, CLICAR, 0&, 0&

End Sub

 

 

 

 
Postado : 14/03/2021 3:39 pm
EdsonBR reacted