copy drawing resources vba randomly crashing

copy drawing resources vba randomly crashing

Anonymous
Not applicable
510 Views
3 Replies
Message 1 of 4

copy drawing resources vba randomly crashing

Anonymous
Not applicable

It could have something to do with the drawing itself - maybe I can turn errors back on in here....

Public Sub CopyDrawingResources()
    If ThisApplication.ActiveDocument Is Nothing Then
        Exit Sub
    Else
        If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
            Dim InvDoc As Document
            Set InvDoc = ThisApplication.ActiveDocument
        Else
            Exit Sub
        End If
    End If
    Dim SourceDoc As DrawingDocument
    Set SourceDoc = ThisApplication.Documents.Open("J:\Inventor Files\Templates\LM Drawing 8_5x11.idw", False)
    
    On Error Resume Next
    For Each Item In InvDoc.SheetFormats
        Item.Delete
    Next
    For Each Item In InvDoc.BorderDefinitions
        If Item.IsReferenced = False Then Item.Delete
    Next
    For Each Item In InvDoc.TitleBlockDefinitions
        If Item.IsReferenced = False Then Item.Delete
    Next
    For Each Item In InvDoc.SketchedSymbolDefinitions
        If Item.IsReferenced = False Then Item.Delete
    Next
    
    For Each Item In SourceDoc.BorderDefinitions
        Set TargetBorder = Item.CopyTo(InvDoc, True)
    Next
    For Each Item In SourceDoc.TitleBlockDefinitions
        Set TargetHeader = Item.CopyTo(InvDoc, True)
    Next
    For Each Item In SourceDoc.SketchedSymbolDefinitions
        Set TargetSymbol = Item.CopyTo(InvDoc, True)
    Next
    For Each Item In SourceDoc.SheetFormats
        'Caution: this creates a new copy of the TitleBlock used for EACH SheetFormat,
        'regardless if the TitleBlockDefinition already exists or not!!!
        Set TargetSheet = Item.CopyTo(InvDoc)
    Next
    On Error GoTo 0
    
    SourceDoc.ReleaseReference
    SourceDoc.Close (True)
    Set SourceDoc = Nothing
    
End Sub

 

here's where it's bombing right now, invalid procedure call or argument

bomb.PNG

 

 

- if I leave the error skipping on, and delete some older templates and borders prior to running this, it will run without crashing.

It looks like it's trying to delete stuff earlier on in the code, maybe I can force this somehow, ie: remove it from all drawing sheets, then the tree, then copy the new stuff.

 

0 Likes
511 Views
3 Replies
Replies (3)
Message 2 of 4

Frederick_Law
Mentor
Mentor

Does it crash because there is a Border with same name in target file?

0 Likes
Message 3 of 4

Anonymous
Not applicable

when I delete the existing border and tb it does work-

 

I know it works when the title block already exists.     I am just hoping I can delete the old border and/or over write.

0 Likes
Message 4 of 4

Frederick_Law
Mentor
Mentor

There is a Default Border which cannot be edit or delete.  You might need to skip that.

0 Likes