Jeff Mishler helped me with this code.
this code will find the last entity, regardless of space or layout. it will then zoom to the last objects bounding box coords.
[code]
'Find last entity added to drawing, zoom to location, and select
'http://discussion.autodesk.com/thread.jspa?messageID=4227426腢
' thanks Jeff Mishler, once again for the code help
Option Explicit
Private Const mconSmallestViewSize As Double = 100
Sub ZoomToLastEntity()
On Error Resume Next
Dim oLayout As AcadLayout
Dim oLayouts As AcadLayouts
Dim oLayoutToRestore As AcadLayout
Dim oEnt As AcadEntity
Dim oEntTemp As AcadEntity
Dim lEntHandle As Long
Dim ltemphandle As Long
Set oLayouts = ThisDrawing.Layouts
' Search through every layout (model and all tabs) for last entity created
For Each oLayout In oLayouts
Set oEntTemp = LastEntity(oLayout.Block)
If Not oEntTemp Is Nothing Then
ltemphandle = CLng("&H" & oEntTemp.Handle)
If ltemphandle > lEntHandle Then
lEntHandle = ltemphandle
Set oEnt = oEntTemp
Set oLayoutToRestore = oLayout
End If
End If
Next
If Not oLayoutToRestore Is Nothing Then
If ThisDrawing.ActiveLayout.Name <> oLayoutToRestore.Name Then ThisDrawing.ActiveLayout = oLayoutToRestore
'shouldn't be here! should always have at least one?
Else
MsgBox "ERROR finding layout object"
End If
'make sure we're in pspace in layout tab. don't want to zoom in a viewport
If oLayoutToRestore.Name <> "Model" Then
ThisDrawing.MSpace = False
End If
If Not oEnt Is Nothing Then
' Zoom to entity
ZoomToEntity oEnt
' display entity info
ThisDrawing.Utility.Prompt "Entity Type: " & oEnt.ObjectName & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Layer: " & oEnt.Layer & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Linetype: " & oEnt.Linetype & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Linetype scale: " & oEnt.LinetypeScale & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Lineweight: " & oEnt.Lineweight & vbCr
AcadApplication.Update
ThisDrawing.Utility.Prompt "Layout: " & oLayoutToRestore.Name & vbCr
AcadApplication.Update
' select object
ThisDrawing.Utility.Prompt "Last object is selected"
AcadApplication.Update
Set oEnt = Nothing
ThisDrawing.SendCommand (".pselect" & vbCr & "last" & vbCr & vbCr)
'shouldn't be here.
Else
MsgBox "No ENTITY FOUND."
End If
Set oEntTemp = Nothing
Set oLayout = Nothing
Set oLayouts = Nothing
Set oLayoutToRestore = Nothing
If Err.Number <> 0 Then Err.Clear
End Sub
Private Sub ZoomToEntity(oEntityToZoomTo As AcadEntity)
Dim vLowerLeft As Variant
Dim vUpperRight As Variant
Dim vViewSize As Variant
Dim dblScaleFactor As Double
' get bounding box of object
oEntityToZoomTo.GetBoundingBox vLowerLeft, vUpperRight
' zoom window with bounding box coords
ZoomWindow vLowerLeft, vUpperRight
' get new view size
vViewSize = ThisDrawing.GetVariable("VIEWSIZE")
'model space resize
If ThisDrawing.ActiveSpace = acModelSpace Then
' if object is small, zoom out a bit
If vViewSize < mconSmallestViewSize Then
dblScaleFactor = vViewSize / mconSmallestViewSize
AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative
End If
' paper space (for us) tends to be much different. our view size is rarely over 30
Else
If vViewSize < mconSmallestViewSize Then
dblScaleFactor = vViewSize / 10
AcadApplication.ZoomScaled dblScaleFactor, acZoomScaledRelative
End If
End If
End Sub
Public Function LastEntity(container As AcadBlock) As AcadEntity
On Error Resume Next
Set LastEntity = container(container.Count - 1)
If Err.Number <> 0 Then Err.Clear
End Function
[/code]