So, let's start with what you had and take a look at it. Starting off with the first sub...Public WithEvents ACADApp As AcadApplication
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Example_AcadApplication_Events()
Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
In order to use an application level event, like BeginQuit, you have set the application. The sub that you have there does set the application. The problem is that before it is set, the sub has to be run and you have done nothing here to cause it to run. I went with the assumption that before you quit autocad, you would close at least one drawing. With that assumption, we can use BeginClose to set ACADApp because BeginClose is a Document level event which initializes automatically when you start. So...Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
Now on to the rest of it.'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
'
MySubmarine
'
If MsgBox("AutoCAD is about to shut down. Do you want to continue with the shutdown?", vbYesNoCancel + vbQuestion) <> vbYes Then
Cancel = True
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MySubmarine()
Kill "c:\MyDamnStupidFile.txt"
End Sub
First off, when you want to run a sub from another sub, you need to call it so instead of the line MySubmarine
you should use Call MySubmarine
The next thing I've got is the message box. Do you really want AutoCAD to prompt you on whether you want to quit or not every time you close it? Assuming you have users, they will kill you for something like that. The message box was in the sample code just to show that you can capture the event and cancel it if you need to. You don't have to have it. So, taking that into consideration, we have this.Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
'
Call MySubmarine
'
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MySubmarine()
Kill "c:\MyDamnStupidFile.txt"
End Sub
Next, why did you separate it out into two subs? I don't see any reason for it but if you had one, I would be interested in hearing it. In the mean time, let's combine those to into one.Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
Kill "c:\MyDamnStupidFile.txt"
End Sub
The last thing I've got for you right now is, When you change versions, will you have only one version in use? Personally, when I upgrade, I install the new version but leave the old version installed while I make sure everything is functioning correctly. I also usually use the new version for production for a while before installing for others, and often install in phases. If there will only ever be one version of autocad using this application at the same time, you can safely leave it as is, and edit the version number as necessary. If there's a chance of multiple versions, then you need some way to differentiate between them. There are a few ways that you can do this. Let's look at the line that sets it.
Set ACADApp = GetObject(, "AutoCAD.Application.16")
We can tell by looking at the application name that what we are passing is a string because it's in quotes. So we need to figure out what the "16" part of it needs to be for it to work with the current version. As we saw earlier in this thread,
Debug.Print ThisDrawing.Application.Version
will give you the version number but it gives more than you need. All we really need to set the application are the two leftmost digits. Now to set the version, we could go a few ways. First off, we could use an If/Then check
If Left(thisDrawing.application.Version,2) = "16" then
Set ACADApp = GetObect(, "AutoCAD.Application.16")
Elseif left(thisdrawing.applicaton.version, 2) = "17" then
Set ACADApp = GetObect(, "AutoCAD.Application.17")
End if
You can see where that would get pretty clunky, pretty fast. A slightly better way would be to use Cases
Select Case Left(ThisDrawing.Application.Version, 2)
Case "16"
Set ACADApp = GetObect(, "AutoCAD.Application.16")
Case "17"
Set ACADApp = GetObect(, "AutoCAD.Application.17")
Case else
Msgbox "the application version wasn't set to " & Left(ThisDrawing.Application.Version, 2)
end Select
Again, requires maintenance and starts to get unwieldy. What we can do is just set the string correctly to begin with.
Set ACADApp = GetObect(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2))
Which overall would give us[code]Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
Set ACADApp = GetObject(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2))
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
Kill "c:\MyDamnStupidFile.txt"
End Sub
Are you still with me? Does it make any sense? Do you have any questions?[/code]