-
Aug 2nd, 2021, 03:59 AM
#1
Thread Starter
New Member
[RESOLVED] Closing a window using VBA
Hi guys
I wonder if anyone can help.
I am trying to close a single excel window using the below code, however it doesn't seem to be doing anything. I works if I want to close say a notepad window, but not Excel. I realise there are other, cleaner, methods like "ActiveWindow.Close" but I specifically need to be able to use the method below.
Is anyone able to point out where I may be going wrong, please? Thanks
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
Sub Closewindow()
Dim Mywin
Mywin = FindWindow(vbNullString, "text work.csv - Excel")
Call SendMessage(Mywin, WM_CLOSE, 0, 0)
End Sub
-
Aug 2nd, 2021, 04:16 AM
#2
Re: Closing a window using VBA
does it find the window? ie. does mywin have a value?
i assume you are using 64bit office?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 2nd, 2021, 04:26 AM
#3
Thread Starter
New Member
Re: Closing a window using VBA
Yes I am using 64 bit office.
It comes up with the message "Cannot quit Microsoft Excel."
-
Aug 2nd, 2021, 05:13 AM
#4
Thread Starter
New Member
Re: Closing a window using VBA
Alternatively, I could use "ActiveWindow.Close", if somehow it could find another instance of excel that is open.
If excel has opened another program of excel, I cannot use "Windows("text work.csv").Activate" to find the workbook. If there is another way to activate it, then this could work?
Thanks to anybody that can help me.
-
Aug 3rd, 2021, 02:59 AM
#5
Thread Starter
New Member
Re: Closing a window using VBA
Sorry, is anybody able to help, please?
-
Aug 3rd, 2021, 03:21 AM
#6
Re: Closing a window using VBA
i do not have 64 bit office, but i you could try some variation of this
Code:
Dim w As Workbook
Set w = GetObject("C:\Users\user\Downloads\text work.csv") ' you must supply the full path to text work.csv
w.Close False
' if the excel window remains open you may need some or any of the below lines
'w.Saved = True
w.Application.DisplayAlerts = False
'w.Parent.Quit
Set w = Nothing
Application.DisplayAlerts = True
if getobject fails i would assume it is a 64 bit thing, but i can not test
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2021, 03:30 AM
#7
Thread Starter
New Member
Re: Closing a window using VBA
Legend, works perfectly. Thank you so much!
-
Aug 5th, 2021, 10:13 AM
#8
Fanatic Member
Re: Closing a window using VBA
Please mark the thread Fixed or watever
yes you wil get a prompt to save the file if changes are made turn alerts off using this method
if you dont care about prompts use kill
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|