Today I’m going to show you how to use a bit of simple VBA to export data to a CSV file. It’s really simple, and although there are other ways of achieving the same thing, this way allows great flexibility, and is very versatile.
Firstly, I’ll set up a drawing with a few points in it. These points will be what we will export:
The next step is to open the VBA editing window using the VBAIDE command. Of course, if you’re using AutoCAD® 2010+ you’ll need to download the VBA add-on. Once open, right click in the project explorer window and create a new module.
Now for the code. I was going to explain the code, but as I’ve commented it pretty well, I don’t really think I need to. Take note of the part about the FileSystemObject, as you’ll need to follow the instructions before the code will work. Do ask if you have any other questions.
Option Explicit Sub ExportPoints() 'Declare variables Dim currentSelectionSet As AcadSelectionSet Dim ent As AcadEntity Dim pnt As AcadPoint Dim csvFile As String Dim FSO As FileSystemObject Dim textFile As TextStream 'Create a reference to the selection set of the currently selected objects Set currentSelectionSet = ThisDrawing.ActiveSelectionSet 'Check if anything is selected, and give exit with a warning if not If currentSelectionSet.Count = 0 Then ThisDrawing.Utility.Prompt "There are no currently selected objects. Please select some points to export, and run this command again." & vbNewLine End If 'Use a For Each statement to look through every item in CurrentSelectionSet For Each ent In currentSelectionSet 'In here, ent will be one of the selected entities. 'If ent is not a point object, we should ignore it If TypeOf ent Is AcadPoint Then 'Only points will make it this far 'Now that we know we are dealing with a point, 'we can use the specific AcadPoint type of variable. Set pnt = ent 'You'll notice that after doing this, you have more 'intellisense methods when you type "pnt." 'Add a line to the string variable csvFile. 'We are concatenating two numbers together with a comma in between, 'and adding a new line character at the end to complete the row. csvFile = csvFile & pnt.Coordinates(0) & "," & pnt.Coordinates(1) & vbNewLine 'Saying that csvFile = csvFile & whatever is a useful 'way to repeatedly add to the end of a string variable. End If Next 'Write the contents of the csvFile variable to a file on the C:\ with the same name 'FileSystemObjects are really useful for manipulating files 'But, you'll need a reference to the Microsoft Scripting Runtime in your VBA project. 'Go Tools>References, and select the Microsoft Scripting Runtime. 'Create a new File System Object Set FSO = New FileSystemObject 'Using FSO.CreateTextFile, create the text file csvFile.csv, 'and store a reference to it in the variable textFile Set textFile = FSO.CreateTextFile("C:\csvFile.csv") 'Write the string variable csvFile to textFile textFile.Write csvFile 'Close textFile, as we are finished with it. textFile.Close 'Alert the user that the file has been created ThisDrawing.Utility.Prompt "Points have been exported to C:\csvFile.csv" & vbNewLine End Sub
And there we have it. You could of course add more conditions in there – say, nest another IF statement in the middle that filters out say only red points. It doesn’t have to be points either – this process will work with any properties of any AutoCAD® entity.
Have fun, and if you haven’t already, please do subscribe below!