Customer Service | Training | Contact Us
You are here: Home > User Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

How to add Shapefile?   Jussi Niilahti Jun 29, 2003
Re: How to add Shapefile?   Dustin Sampson Jul 03, 2003
Report Inappropriate Content • Top • Print • Reply    
Subject How to add Shapefile? 
Author Jussi Niilahti 
Date Jun 29, 2003 
Message How can I add specific Shapefile to TOC. Is there some VBA sub for that? (I would like to add for example
c:\mydocuments\shapes\myshapefile.shp to arcmap automatically by pressing the specified button).

Thanks! 
   
Report Inappropriate Content • Top • Print • Reply    
Subject Re: How to add Shapefile? 
Author Dustin Sampson 
Date Jul 03, 2003 
Message Try something like this... 
 
Private Sub addShapeFileLayer()
  Dim pWorkspaceFactory As IWorkspaceFactory
  Dim pFeatureWorkspace As IFeatureWorkspace
  Dim pFeatureLayer As IFeatureLayer
  Dim pMxDocument As IMxDocument
  Dim pMap As IMap
On Error GoTo ErrorHandler
'Create a new ShapefileWorkspaceFactory object and open a shapefile folder
  Set pWorkspaceFactory = New ShapefileWorkspaceFactory
  Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("C:\temp\", 0)
  'Create a new FeatureLayer and assign a shapefile to it
  Set pFeatureLayer = New FeatureLayer
  Set pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pLayer.Name)
  pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName

  'Add the FeatureLayer to the focus map
  Set pMxDocument = Application.Document
  Set pMap = pMxDocument.FocusMap
  pMap.AddLayer pFeatureLayer
  
  pMap.MoveLayer pFeatureLayer, (pDoc.FocusMap.LayerCount)
ErrorHandler:
Exit Sub
End Sub
 
  Dustin