Message 1 of 8
Set ActiveDocument question

Not applicable
07-06-2006
10:08 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")
the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?
Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)
'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate
Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)
now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked
however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.
Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.
This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.
any ideas?
heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post
Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If
Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate
'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If
'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer
'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If
If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '
Loop
Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function
I searched hundreds of posts on google first but didn't find anything about
this
probably used wrong search term("ActiveDocument")
the line below sometimes works and sometimes doesn't
does any one have the secret to how to wait for acad to realize it's changed
it's activeDoc?
Dim oDoc as AcadDocument
Dim oAcad as AcadApplication
Dim sSettingDwgNameas String
'(sSettingDwgName is open in editor at time this sub is run)
'here's the code that "should" switch documents....
oAcad.ActiveDocument = oAcad.Documents.Item(sSettingDwgName)
' - or equaly valid
oAcad.Documents.Item(sSettingDwgName).Activate
Set oDoc = oAcad.ActiveDocument
'now theoretically oDoc.Name = sSettingDwgName....but noooooooooooooo
:-)
now here's the deal, the acad editor visually always switches immediately to
the new dwg (sSettingDwgName) as if the line setting the active document
actually worked
however, code execution using the oDoc reference sometimes (70% of the time
approx) still points to the doc from which the code originated.
Obviously i have to put a timer or something in there to wait for acad to
figure out where it is, but I haven't succeeded in getting any thing to work
even 90% of the time.
This is just for a temporary routine where I select something in one doc and
then switch to another doc to zoom to a corresponding item.
any ideas?
heres a quickie function i'm trying to use
I'm not checking if doc is open because I know it is, in an actual function
i'd put more error trapping in, just keeping it simple for this post
Function SwitchToDoc(sDwgName As String, ByRef odoc As AcadDocument) As
Boolean
Dim oAcad As AcadApplication
Set oAcad = odoc.Application
'sometimes this works, sometimes it doesn't work
'doc changes in editor but focus is still on previous document
Dim dThisTime As Double
Dim dTryTime As Double
dTryTime = 5 'seconds
Dim dStartTime As Double
Dim dElapsed As Double
'IF YOU'RE ALREADY HERE, NOTHING TO DO
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If
Debug.Print "Before changing doc, name is " & oAcad.ActiveDocument.Name
'either of these *should* work
'oAcad.ActiveDocument = oAcad.Documents.Item(sDwgName)
oAcad.Documents.Item(sDwgName).Activate
'IF THAT WORKED, WE'RE DONE
If oAcad.ActiveDocument.Name = sDwgName Then
Set odoc = oAcad.ActiveDocument
Debug.Print "oDoc: " & odoc.Name
SwitchToDoc = True
Exit Function
End If
'SADLY MOST OF THE TIME IT DOESN'T WORK
Debug.Print "after changing doc, name is still " & odoc.Name
If odoc.Name <> sDwgName Then
dStartTime = Timer
'to avoid infinite loop
Do While dElapsed < dTryTime
If oAcad.GetAcadState.IsQuiescent = True Then
Debug.Print "ready"
SwitchToDoc = True
Exit Do
Else
Debug.Print "Not ready, timer: " & dThisTime
End If
DoEvents
Debug.Print " oDoc: " & odoc.Name
dThisTime = Timer
dElapsed = dThisTime - dStartTime
If odoc.Name <> sDwgName Then
Debug.Print "Still not right dwg <" & dElapsed & ">"
Else
Debug.Print "Finally that changed "
SwitchToDoc = True
Exit Do
End If
If dElapsed >= dTryTime Then
Debug.Print "Time ran out for doc change"
Exit Function
End If '
Loop
Else
Debug.Print "Doc change worked first time "
SwitchToDoc = True
End If
End Function