Export parts list as .csv to specific folder.

Export parts list as .csv to specific folder.

jostroopers
Collaborator Collaborator
1,882 Views
5 Replies
Message 1 of 6

Export parts list as .csv to specific folder.

jostroopers
Collaborator
Collaborator

Hi.

I want to export my parts list to a .csv file and that is working.

But i want to save the .csv file to the folder: Z:\Uni_Link\Import\CSV

I have tried this with this code but I cannot get it done.
The .csv file is now always saved in the folder that also contains the .dwg in which the parts list was created.

 

Dim oDoc As Inventor.DrawingDocument
oDoc = ThisDoc.Document
path_and_name = ThisDoc.PathAndFileName(False)' without extension

Dim oSheet As Inventor.Sheet
'oSheet = oDoc.Sheets(1) ' first sheet
oSheet = oDoc.Sheets("Sheet:1") ' sheet by name

' say there is a Partslist on the sheet.
Dim oPartslist As PartsList
oPartslist = oSheet.PartsLists(1)

''set the folder to save to
'Dim sFolder As String
'sFolder = Left(sPath, InStrRev(sPath, "\")) & "Z:\Uni_Link\Import\CSV"

''check for the folder and create it if it does not exist
'If Not System.IO.Directory.Exists(sFolder) Then
'System.IO.Directory.CreateDirectory(sFolder)
'End If

' export the Partslist to Excel.
oPartslist.Export(path_and_name & ".csv", PartsListFileFormatEnum.kTextFileCommaDelimited, oOptions) 
Mvg Jos
Youre drawings are as good as the symbols that compleet them.....
0 Likes
Accepted solutions (1)
1,883 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Try this:

(I'm also showing how to specify the options, but have that portion commented out, due to how custom those setting are.)

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("This rule only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
Dim oPath As String = "Z:\Uni_Link\Import\CSV\"
Dim oNewFullName = oPath & oFileName & ".csv"
Dim oPartslist As PartsList = oDDoc.Sheets.Item("Sheet:1").PartsLists.Item(1)
If Not IO.Directory.Exists(oPath) Then
	IO.Directory.CreateDirectory(oPath)
End If
If IO.File.Exists(oNewFullName) Then
	oAns = MsgBox("That file already exists. Do you want to overwrite it?", vbYesNo + vbQuestion, " ")
	If oAns = vbNo Then Exit Sub
End If
oPartslist.Export(oNewFullName, PartsListFileFormatEnum.kTextFileCommaDelimited)

'Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'oOptions.Add("TableName", "Parts List for " & oFileName)
'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)
'oPartslist.Export(oNewFullName, PartsListFileFormatEnum.kTextFileCommaDelimited, oOptions)

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

jostroopers
Collaborator
Collaborator

@WCrihfield  thanks.

Great, this is exactly what I needed.
I am not yet using the options.
Will see if it helps me at a later stage.

Thank you very much again.
And keep giving this support, this  helps us further to make great designs.

 

Mvg Jos
Youre drawings are as good as the symbols that compleet them.....
0 Likes
Message 4 of 6

zaidi_drafter
Explorer
Explorer

Hi,

 

I have run the ilogic , got error. Please help

0 Likes
Message 5 of 6

zaidi_drafter
Explorer
Explorer
 

image.pngScreenshot 2026-02-25 134136.png

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

0 Likes