Close worbook vba not working. (1 Viewer)

MsAccessNL

Member
Local time
Today, 08:40
Joined
Aug 27, 2022
Messages
219
Sub Closethis()
Thisworkbook.close
End sub

Doesn’t work if this sub is called from another function or from an event. Only if you run it direct. Chat said to run the code on time, but also nothing.

I have code to check for a value otherwise it will close the workbook. Any ideas to get this working?
 
Try Renaming your Closethis() sub to anything other than that name, ie:

Sub subCloseThis()
Thisworkbook.Close
End Sub
 
Can you give an example of the calling code - the full calling code?
You might consider using true or false for saving
 
Sub CloseThis()
Application.OnTime Now + TimeValue("00:00:01"), "ActualClose"
End Sub

Sub ActualClose()
ThisWorkbook.Close SaveChanges:=False
End Sub
 
Just remember that "ThisWorkBook" is like the Access keyword "Me" in that it is dependent on the context of where it is used. For instance, you cannot use Me.Close from a utility routine in a general module. Further, "Me" can have different meanings at the same time depending on context (if you have two forms open at once). If you have the subroutine in a place different than the place where the workbook was opened, ThisWorkBook MIGHT be out-of-scope. That's why my special object support subs always take an argument as the name of the object or a ByRef type reference to the object. You can't rely on the special-case shortcuts if you have multiple potential scopes active at the same time.
 
Just remember that "ThisWorkBook" is like the Access keyword "Me" in that it is dependent on the context of where it is used. For instance, you cannot use Me.Close from a utility routine in a general module. Further, "Me" can have different meanings at the same time depending on context (if you have two forms open at once). If you have the subroutine in a place different than the place where the workbook was opened, ThisWorkBook MIGHT be out-of-scope. That's why my special object support subs always take an argument as the name of the object or a ByRef type reference to the object. You can't rely on the special-case shortcuts if you have multiple potential scopes active at the same time.
agreed. this is why i asked for the calling code in case he's running automation from another program
 

Users who are viewing this thread

Back
Top Bottom