AGRC Administrator: Written by
AGRC Administrator: Written by
This VBA script demonstrates how to read attributes from an ArcMap Layer and write them to a text file. Note: After copying and pasting this script into your VBA editor, you may have to do a find/replace on the single quote and double quote characters to get replace the slanting style quotes with standard style, straight quotes.
Public Sub DumpAttributesToFileExample() 'Open a new text file to write to Open "c:/temp/outfile.txt" For Output As #1 Dim pMxDoc As IMxDocument Dim pMap As IMap Dim pFLayer As IFeatureLayer Dim pFClass As IFeatureClass Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap 'Get reference to layer in table of contents (0 is topmost layer) Set pFLayer = pMap.Layer(0) Set pFClass = pFLayer.FeatureClass 'Use a Query Filter to select a subset of features using 'a simple SQL where clause Dim pQFilter As IQueryFilter Set pQFilter = New QueryFilter pQFilter.WhereClause = "" 'Establish a cursor used for looping Dim pFCursor As IFeatureCursor Set pFCursor = pFClass.Search(pQFilter, True) Dim pFeature As IFeature Set pFeature = pFCursor.NextFeature 'Loop thru all features and write attributes to output file Do Until pFeature Is Nothing
'Note: below, pFeature.value(1) and pFeature.value(2) use field index numbers of the 2nd and
3rd fields in the table 'to write the values in these fields to output file. Also, the _ character continues an expression to the next line
Print #1, "Field #2 = " & pFeature.Value(1) & " and Field #3 = " & pFeature.Value(2) Set pFeature = pFCursor.NextFeature Loop Close #1 End Sub