Home arrow GIS Data & Resources arrow Scripts and Code arrow Visual Basic / VBA arrow VBA: ArcMap, Loop Through Layer Example
VBA: ArcMap, Loop Through Layer Example PDF Print E-mail

Written by Bert Granberg,

This is a simple VBA script to set up a feature cursor on an ArcMap layer and dump out the values is a couple fields. It is posted here as an example and also a stub as many useful function draw on this simple code to oop through features in a layer.

Public Sub LoopThruLayerAndExportAttributes()

  'open a new text file to write the output text 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 'This is a module-level variable!
  Set pMap = pMxDoc.FocusMap
  Set pFLayer = pMap.Layer(0)
  Set pFClass = pFLayer.FeatureClass

  Dim pQFilter As IQueryFilter
  Dim pFCursor As IFeatureCursor
  Set pQFilter = New QueryFilter
  pQFilter.WhereClause = ""

  Set pFCursor = pFClass.Search(pQFilter, True)
  Dim pFeature As IFeature
  Set pFeature = pFCursor.NextFeature
 
  Do Until pFeature Is Nothing

    'print a line to the new text filewith the values in teh fourth and fifth fields
    Print #1, pFeature.Value(3) & "  " & pFeature.Value(4)
    Set pFeature = pFCursor.NextFeature

  Loop

  Close #1

End Sub


Users' Comments  
 

Display 1 of 1 comments

1. Mon, 06-09-2008 at 07:48 AM

Hi Bert, 
I am a python person and not so good at VB. I have borrowed some VB code from a ESRI thread Link Text that works perfect for one selected polygon of a feature class. Tried to incorporate your the looping structure you preview here and am getting a method not found compile error for the underlined statement in the following. Method calls are different in VB 
Public Sub GridQuadrilateral_test() 
Dim pMxDoc As IMxDocument 
Dim pFLayer As IFeatureLayer 
Dim pFtrSel As IFeatureSelection 
Dim pOutFtrLyr As IFeatureLayer 
Dim pPolygon As IPolygon 
Dim pSegColl As ISegmentCollection 
Dim lIdx As Long 
Dim lCornerIdx(3, 1) As Double 
Dim l As Long 
 
' Get the first selected polygon on the first layer 
Set pMxDoc = ThisDocument 
Set pFLayer = pMxDoc.FocusMap.Layer(0) 
Set pFClass = pFLayer.FeatureClass 
 
Dim pQFilter As IQueryFilter 
Dim pFCursor As IFeatureCursor 
Set pQFilter = New QueryFilter 
pQFilter.WhereClause = "" 
 
Set pFCursor = pFClass.Search(pQFilter, True) 
Dim pFeature As IFeature 
Set pFeature = pFCursor.NextFeature 
Do Until pFeature Is Nothing 'Loop is set up but how do I incorporate the following??? 
Set pFtrSel = pFLayer 
Set pPolygon = pFeature.FeatureClass.GetFeatu re(pFtrSel.SelectionSet.IDs.Ne xt).Shape 
Set pSegColl = pPolygon 
 
Dont know if you could steer me true here? 
Thanks 
Chris

Display 1 of 1 comments

Add your comment

13, Mar. 2008
Last Updated ( 13, Mar. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for