Add a Shapefile Programmatically


This sample code adds a shapefile to the focus map.

How to use:
  1. Paste the code into VBA.
  2. Modify the code to point to a shapefile on your system.
  3. Run the routine from the Macros dialog.
Public Sub AddShapeFile()
  Dim pWorkspaceFactory As IWorkspaceFactory
  Dim pFeatureWorkspace As IFeatureWorkspace
  Dim pFeatureLayer As IFeatureLayer
  Dim pMxDocument As IMxDocument
  Dim pMap As IMap
  'Create a new ShapefileWorkspaceFactory object and open a shapefile folder
  Set pWorkspaceFactory = New ShapefileWorkspaceFactory
  Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("c:\arcgis\arcexe83\ArcObject Developer Kit\Data\World", 0)
  'Create a new FeatureLayer and assign a shapefile to it
  Set pFeatureLayer = New FeatureLayer
  Set pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass("Country")
  pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
  'Add the FeatureLayer to the focus map
  Set pMxDocument = Application.Document
  Set pMap = pMxDocument.FocusMap
  pMap.AddLayer pFeatureLayer
End Sub