Hi @zaidi_drafter. There are multiple possible reasons why that code example might have failed at Line 9. Your drawing may have not had a Sheet named "Sheet:1", or it may have not had a PartsList on that specific Sheet. Sometimes we have to carefully read through each line of a code example before we try it, to see if anything looks incorrect or potentially harmful, with our current custom set of circumstances in mind. I took the time to rewrite that code example in a more robust way, and added in a lot of 'comments' to help describe what the next line of code is about to try to do. I added in some extra 'checks' to help avoid potential errors, along with some feedback when an error is encountered, that will help explain what went wrong.
'try to Cast current document to DrawingDocument Type
'if that fails, no error, but no values gets assigned to variable
Dim oDDoc As Inventor.DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
'if no value got assigned to the variable, let user know, then exit rule
If oDDoc Is Nothing Then
MsgBox("DrawingDocument not obtained - exiting rule!", vbCritical, "NO DRAWING OBTAINED")
Exit Sub
End If
'the following methods will not work, if the drawing has not been saved yet
If oDDoc.FileSaveCounter = 0 Then
MsgBox("This Drawing document must be saved first, before exporting its PartsList to CSV file!", vbCritical, "SAVE DOCUMENT FIRST")
Exit Sub
End If
'get full path of this drawing file, without file name
Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
'add a sub folder named "CSV" to the path
sPath = System.IO.Path.Combine(sPath, "CSV")
'make sure that sub folder exists, if not then create it
If Not System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End If
'get just the name of this drawing file, without any path, and without file extension
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
'combine path & file name
Dim sNewFullName As String = System.IO.Path.Combine(sPath, sFileName & ".csv")
'check if that file already exists
If System.IO.File.Exists(sNewFullName) Then
'if it does exist, let user know, and ask if they want to overwrite it
Dim oAns As MsgBoxResult = MsgBox("That file already exists." & vbCrLf & _
"Do you want to overwrite it?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Overwrite File?")
'if the user said No, then exit this rule
If Not oAns = MsgBoxResult.Yes Then Exit Sub
End If
'get the first PartsList found in this drawing file
'start searching on the currently active sheet, then start searching from first sheet
Dim oPartslist As Inventor.PartsList = Nothing
If oDDoc.ActiveSheet.PartsLists.Count > 0 Then
oPartslist = oDDoc.ActiveSheet.PartsLists.Item(1)
Else
For Each oSheet As Inventor.Sheet In oDDoc.Sheets
If oSheet.PartsLists.Count > 0 Then
oPartslist = oSheet.PartsLists.Item(1)
'now that we have found one, exit this iteration
Exit For
End If
Next 'oSheet
End If
'if no PartsList was found, then let user know, then exit this rule
If oPartslist Is Nothing Then
MsgBox("No PartsList Found!", vbCritical, "No PartsList Found")
Exit Sub
End If
'if specifying some options, we need to create an Inventor.NameValueMap object
'then add some (name - value) entries into it matching available option names & possible values
'Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'oOptions.Add("TableName", "Parts List for " & sFileName)
'oOptions.Add("ExportedColumns", "Column 1;Column 2;Column 3;Column 4;Column 5")
'oOptions.Add("IncludeTitle",True)
'oOptions.Add("StartingCell", "A1")
'oOptions.Add("Template", "C:\Temp\CSV Parts List Template.csv")
'oOptions.Add("AutoFitColumnWidth",True)
'try to export this PartsList to a CSV file
'put something that might 'fail' or cause an error on the 'Try' side
'then an alternative action or feedback message on the 'Catch' side
'if the code within the 'Try' side fails, it will not throw an error,
'but when the Try side code fails, then the code within the 'Catch' side will run instead
Try
'export without specifying any options
oPartslist.Export(sNewFullName, PartsListFileFormatEnum.kTextFileCommaDelimited)
'or export with specifying some options
'oPartslist.Export(sNewFullName, PartsListFileFormatEnum.kTextFileCommaDelimited, oOptions)
Catch ex As Exception 'capturing the actual Exception object to a variable here
MsgBox("Error exporting PartsList to CSV file!" & vbCrLf & _
ex.Message & vbCrLf & ex.StackTrace)
End Try
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)