Useful Scripts for the Non-Programmer: Getting Attributes from Selected Features

October 31, 2011 — Corey Blakeborough

Obtaining a useful report from ArcMap can be a long and complicated process that needs a script in order to be executed properly. Using the script from this article, simple reports can easily made from any set of features that you select!

This is another VBA module that will take a list of features that you currently have selected and export a semicolon-delimited list of the following attributes of each feature:

  • Feature Class
  • OID
  • Any one attribute (you choose!)

You’ll find the results waiting for you in a text file once you run the report. Ready to give it a try? Let’s get the module up and running.

  • Within ArcMap, drop down the Tools menu and choose MacrosVisual Basic Editor.
  • In the editor, click ToolsReferences and ensure that the Microsoft Scripting Runtime reference is enabled.


  • Within the Visual Basic environment, expand the Normal.mxt file. Right-click it and insert a new Module within the Modules folder.

The code for this module is below. Copy and paste it into the module and save it, but continue reading for configuration information.

Private Const FIELDNAME As String = “SubtypeCode
Private Const FILENAME As String = “C:\Temp\featureAttributeExport.txt

Public Sub GetFeatureAttributes()

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim writer As TextStream
Set writer = fso.CreateTextFile(FILENAME, True)

Dim mxdoc As IMxDocument
Set mxdoc = ThisDocument

Dim map As IMap
Set map = mxDoc.FocusMap

Dim eSel As IEnumFeature
Set eSel = map.FeatureSelection
Dim eSetup As IEnumFeatureSetup
Set eSetup = eSel
eSetup.AllFields = True

Dim f As IFeature
Set f = eSel.Next
While Not f Is Nothing
writer.WriteLine f.Class.AliasName & “;” & f.OID & “;” & f.Value(f.Fields.FindField(FIELDNAME))
Set f = eSel.Next
Debug.Print “Results written to ” & FILENAME
End Sub

Before you go off and run the script as-is, be sure to configure the values first. The first two lines define constants called FIELDNAME and FILENAME. Change the values inside the parentheses to customize the script to your liking:

FIELDNAME corresponds to the name of the database field that you want to export in addition to the OID and the feature class name. If you’re unsure of the exact name, right-click a feature layer and choose Properties, then go to the Fields tab to see exact field names.

Importantly, if you’re reporting on data from more than one feature class, be sure that the field name applies to every class in your selection! If this is not the case, the script will throw an error or you’ll see blank data. Unless you’re using a consistently-named field such as a feature’s subtype code, you should try to only select one layer at a time to run this script. A quick way to accomplish this is to right click a layer, and choose Selection → Make This The Only Selectable Layer.



FILENAME corresponds to the exact location the script will place the file when you run it. This can be left at its default value if desired. Look at this value to see exactly where to look for your text file. Please note that this script will overwrite the text file from any previous run, so if you are running multiple exports, be sure to save the text file elsewhere after each run.

After you tweak these two settings to your liking, you can run the script! Switch over to ArcMap and use the Select Tool to grab the features you want. Then switch back over to the Visual Basic Editor and press the play button.

That’s it! Your shiny new export will be located in the configurable directory location at the top of the script.

We Wrote the Book

The Indispensible Guide to ArcGIS Online

Download It for Free

Corey Blakeborough

Corey Blakeborough is a Team Lead, Senior Consultant at the Utility & Telecommunications GIS consulting company SSP Innovations in Centennial, Colorado. Corey has over five years of experience with SSP, providing a variety of GIS and work management solutions.

One comment

  • Great work, happy to have come across this blog on blogs list.
    This is the sort of knowledge that should be distributed around the
    internet. You should really consider writing more things like this on web
    2.0 list. It’s a shame that the search engines aren’t giving a higher position. Thank you

What do you think?

Leave a comment, and share your thoughts

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>