0% found this document useful (0 votes)
19 views2 pages

AGRC Administrator: Written by

This VBA script demonstrates how to read attributes from an ArcMap layer and write them to a text file. The script opens a text file, gets references to the map and layer, establishes a cursor to loop through features, and writes the attribute values of specified fields to the text file for each feature. It loops through all features, retrieves the field values using indexes, and prints them to the file with field labels before moving to the next feature.

Uploaded by

Vinod Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

AGRC Administrator: Written by

This VBA script demonstrates how to read attributes from an ArcMap layer and write them to a text file. The script opens a text file, gets references to the map and layer, establishes a cursor to loop through features, and writes the attribute values of specified fields to the text file for each feature. It loops through all features, retrieves the field values using indexes, and prints them to the file with field labels before moving to the next feature.

Uploaded by

Vinod Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ArcObjects VBA Example: Write Attributes to a Text File

Written by AGRC Administrator,

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

You might also like