Author Topic: UserForm with BeginQuit  (Read 3142 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« on: July 16, 2005, 05:46:16 AM »
Hello everybody,

I have a Userform I want to run with the BeginQuit event.
I placed the event handler in the ThisDrawing Module.

Everthing works fine, as long as there is a drawing open, so I do not close all the drawings first.
The event is still firing, I tried with a MsgBox, but it can’t find the Userform.

How can I link that?

All my Thanks in Advance

Bernd
Bernd

Murph

  • Bull Frog
  • Posts: 402
  • P.I.T.A.
UserForm with BeginQuit
« Reply #1 on: July 16, 2005, 08:56:27 AM »
From the BeginQuit Sub you will have to call the userform like this:

UserFormName.Show
Murphy's Law applies to all.
Whatever can go wrong, will, and at the most inopportune time.

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #2 on: July 16, 2005, 09:53:57 AM »
That's what i do
Quote

UserForm1.Show

and i get
Quote
Failed to get theDocument object


als Error.

Bernd
Bernd

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16723
  • Superior Stupidity at its best
UserForm with BeginQuit
« Reply #3 on: July 16, 2005, 01:46:39 PM »
The reason is that there is no document loaded. To make it work properly, you need to create your own subclass of the AcadApplication and add your events to it.

AHHHHHH...

no, really it isn't that bad ...

in fact it is quite simple ....

In your VBA Editor:
Insert->Class Module

In the new class module (named Class1) add this code.

Code: [Select]
Option Explicit
'our event app outside of ThisDrawing
Private WithEvents myApp As AcadApplication

'Our instance of BeginQuit
Private Sub myApp_BeginQuit(Cancel As Boolean)
' put the code here for the UserForm you want to show
  UserForm1.Show
End Sub

' define our single property of the class ...
' in this instance it is an AcadApplication object
' this must be set in your module
Public Property Set App(NewApp As AcadApplication)
   'set the private class AcadApplication
    Set myApp = NewApp
End Property



But we are not yet done ...

Insert->Module

In your new module (or an existing one if you have it created already)
Code: [Select]
Option Explicit
'IMPORTANT!! 'NewApp' MUST be set as a GLOBAL variable
'otherwise when the subroutine ends the class is terminated
'because the 'NewApp' variable is cleared
Public NewApp As New Class1

'Set our "hook" as they are called .. into the main application
'by setting our new NewApp class object to
'the current AcadApplication
Sub HookApp()
 Set NewApp.App = AcadApplication
End Sub


Now that you know how to do it .. it doesn't seem so bad now does it ...

In fact, if you select your dropdown combo in the VBAIDE while in the Class code window, you will be able to see all of the application defined events and use them as you see fit, outside of the 'ThisDrawing' code window.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #4 on: July 16, 2005, 04:36:38 PM »
Keith,

removed my BeginQuit sub from the ThisDrawing Module and ran
your code, it works the same with one dwg still open and gives the same
error without the dwg.

Thanks, Bernd
Bernd

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16723
  • Superior Stupidity at its best
UserForm with BeginQuit
« Reply #5 on: July 16, 2005, 04:49:48 PM »
remember you MUST put the first code in a Class module and you MUST put the second code in a regular Module, you CANNOT put it in the ThisDrawing code window.

ALSO

Remember you CANNOT access ThisDrawing ANYWHERE in this code, if you do it will return an error, because there IS NO document.

The code is good, I suspect you have implemented it incorrectly. If you can take a screen shot of the window where you put the code and your implementation of it, I am sure I can help you get it going.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #6 on: July 16, 2005, 05:01:20 PM »
Keith,
I can't paste a screenshoot here. But i ocopied and pasted the code you wrote in way you wrote. But also when i'm in my class module and look in th dropdown I see only the sub defined and not the events like i see in ThisDrawing.

I just placed a MsgBox before the UserForm1.Show and this shows.

So the event is fireing, but the Userform..

Thanks Bernd
Bernd

Jeff_M

  • King Gator
  • Posts: 3945
  • C3D user & customizer
UserForm with BeginQuit
« Reply #7 on: July 16, 2005, 05:43:12 PM »
Hi Bernd,
Out of curiosity I just tried Keith's code here in R2002. All I did was follow Keith's directions, and added a call to the HookApp in my AcadStartup Sub. It works great in both cases (0 or 1 dwg open).

Is there code in your form that does something strange? For my form, I just used an empty form with a Done button that executes Unload Me. Maybe paste your Form's code here to look at....

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #8 on: July 17, 2005, 03:33:38 AM »
:oops:
Jeff,
You are damn right.

I guess it was a little bit late here in Europe last evening when I was running this code.

Tried the same as you (simple UserForm) and it works. I used some functions in my code to find files
http://www.theswamp.org/phpBB2/viewtopic.php?t=5434

and of course, there was the ThisDrawing error in it.
Quote

Sorry Keith, your stuff always works, I know. I did not mean to question that, but this VBA is killing me.


Since I need this code in findfile to read out the search paths (in our Company in different Offices the maintain different structures), I ‘m a little stuck in a dilemma.

Maybe I will check in the BeginClose Event for the Document count and if 0 I will open a new dwg just for the sake of the ThisDrawing event.

Any better ideas are welcome like water in the desert.

Thanks a lot

Bernd
Bernd

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16723
  • Superior Stupidity at its best
UserForm with BeginQuit
« Reply #9 on: July 17, 2005, 12:43:10 PM »
Why are you using ThisDrawing at all? If there is no open document you don't need to access ThisDrawing.

I will try and help you resolve the problem if you tell us what you are trying to do.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Jeff_M

  • King Gator
  • Posts: 3945
  • C3D user & customizer
UserForm with BeginQuit
« Reply #10 on: July 17, 2005, 02:02:36 PM »
Quote from: Amsterdammed
:oops:
Jeff,
You are damn right.

Since I need this code in findfile to read out the search paths (in our Company in different Offices the maintain different structures), I ‘m a little stuck in a dilemma.

Any better ideas are welcome like water in the desert.

OK, I see (I think) what you need......based on Bob Wahr's code in the thread you mention, since you won't have a drawing open you can ditch the portions that use ANYTHING about Thisdrawing, or the current drawing's path. Which leaves you only needing to know the Acad search paths for files. So in place of:
strSupPth = ThisDrawing.GetVariable("acadprefix")
use this:
strSupPth = AcadApplication.Preferences.Files.SupportPath

HTH,
Jeff

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #11 on: July 17, 2005, 02:35:23 PM »
Jeff,
you saw my problem.
What should i do about the

strDwgDir = ThisDrawing.GetVariable("dwgprefix")

in Bob Wahr's code?

May be it would be easier to let my main.fas function, witch loads my codes, copy the files i need to search for to C:\Windows\temp (one of the few paths everybody can write in my company to) and set this path into my VBA code.

It would work than quicker, too I guess?

What do you think?

Bernd
Bernd

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #12 on: July 17, 2005, 02:57:26 PM »
Jeff,
it works,

I did what you wrote and replaced the dwgprefix thing (i don't want  to search there any way) with c:\windows\temp.

Thanks a lot for the Sunday overhours
 :D
Bernd

Jeff_M

  • King Gator
  • Posts: 3945
  • C3D user & customizer
UserForm with BeginQuit
« Reply #13 on: July 17, 2005, 03:05:47 PM »
Quote from: Amsterdammed
Jeff,
you saw my problem.
What should i do about the

strDwgDir = ThisDrawing.GetVariable("dwgprefix")

in Bob Wahr's code?

May be it would be easier to let my main.fas function, witch loads my codes, copy the files i need to search for to C:\Windows\temp (one of the few paths everybody can write in my company to) and set this path into my VBA code.

It would work than quicker, too I guess?

What do you think?

Bernd
I think that Keith is right. You need to post what exactly it is you are trying to do. As I said, just ditch (remove) anything that has to do with a drawing. So that would mean a function that looks like this:
Code: [Select]

Private Function FindFile(strFile As String)
Dim strSupPth As String
Dim varPaths As Variant
Dim intCnt As Integer
Dim strFnd As String
Dim strTest As String

strSupPth = AcadApplication.Preferences.Files.SupportPath
strTest = Dir(CurDir & "\" & strFile, 31)

If strTest <> "" Then
    strFnd = CurDir & "\" & strTest
Else
    If strSupPth <> "" Then
        varPaths = Split(strSupPth, ";", -1, vbTextCompare)
        For intCnt = 0 To UBound(varPaths)
            strTest = Dir(varPaths(intCnt) & "\" & strFile, 31)
            If strTest <> "" Then
                strFnd = varPaths(intCnt) & "\" & strFile
                Exit For
            End If
        Next intCnt
    End If
End If

FindFile = strFnd

End Function

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
UserForm with BeginQuit
« Reply #14 on: July 17, 2005, 03:44:13 PM »
Jeff,

I did what you wrote, plus i ditched the dwgprefix; i didn't want to search there in the first place.

This works now perfect.

Thanks Bernd :D
Bernd