Close a windows window by its title

Hi all

I'm looking for how to close a Windows window just with the title of the window.

I do have this code, but I have an error " user-defined type not defined " on " Dim w As Window "

Dim w As Window, Ligne As Integer
Ligne = 1
For Each w In Windows
Cells(Ligne, 1) = w.Caption
Ligne = Ligne + 1
Next w
Window.Activate ("Bonjour")
Window.Close ("Bonjour")

I think it's certainly a function or a reference to add, but I can't find which one.

Do you have an idea?

Hello

Unless I'm mistaken, it's an Excel function but it doesn't work on SW.
You should look at this topic and adapt the proposed code to be able to intercept the window and close Centering a userform on a screen - Macro - myCAD Forum

2 Likes

Hello;
What kind of " Windows " window do you want to close?
it is possible to inhibit some of them with the management of " Warnings " in the solidworks APIs:
https://help.solidworks.com/2016/English/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swFileSaveWarning_e.html
or by using the " Silent " function:

Yes absolutely, I did some research on VBA code and I thought it could work.
Of course not.

It is a pop-up window of an external software.

I managed to get away with this code:

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, _
    ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
     lParam As Any) As Long

Const WM_CLOSE = &H10
Dim Mywin
    Mywin = FindWindow(vbNullString, "Make a donation")
    Call SendMessage(Mywin, WM_CLOSE, 0, 0)

@treza88 if the problem is solved, you can select your own answer as the best answer in order to close the topic and help another user who has the same need if necessary.

1 Like