Message 1 of 8
Object reference error when activating a drawing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I wrote some code to make drawings of a part file using a template but I am running into this error: "Object reference not set to an instance of an object." when I try to activate a sheet of the DrawingDocument. Below is my code:
' Get the active part document Dim partDoc As PartDocument partDoc = ThisApplication.ActiveDocument ' Get the part file name without extension Dim partFileName As String partFileName = System.IO.Path.GetFileNameWithoutExtension(partDoc.FullFileName) ' Get the folder path of the part Dim partFolderPath As String partFolderPath = System.IO.Path.GetDirectoryName(partDoc.FullFileName) ' Specify the path to your drawing template file Dim sortTemplatePath As String sortTemplatePath = "C:\Users\Public\Documents\Autodesk\Inventor 2023\Templates\en-US\Standard.dwg" ' Create the drawing file name by appending ".idw" Dim sortFileName As String sortFileName = System.IO.Path.Combine(partFolderPath, partFileName & "-DRAWING.dwg") ' Get the active drawing document Dim sortDwg As DrawingDocument ' Check if the drawing already exists If System.IO.File.Exists(sortFileName) Then ' If the drawing exists, open it ThisDoc.Launch(sortFileName & DWGType) Else ' If the drawing doesn't exist, create a new drawing from the template sortDwg = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,sortTemplatePath,True) 'sortDwg = ThisApplication.Documents.Open(sortTemplatePath, False) ' Save the drawing with a new name sortDwg.SaveAs(sortFileName, False) ThisDoc.Launch(sortFileName & DWGType) End If sortDwg.Activate() 'This is the line that gives me the error Dim oSheet As Sheet = sortDwg.Sheets.Item(1)
Any help would be appreciated! Thank you!