HOW TO
This article provides a code sample showing how to change the feature class which a layer file references. This would be useful if the feature class had been renamed or moved and the link between the layer file and the data needed to be recreated.
Note:
Code in the Project's 'ThisDocument' code module will only run in the current map document. If it is necessary to store the code in all map documents, open the Normal.mxt ThisDocument code module instead.
Code:
Public Sub test()
' open existing lyr file.
Dim pGxLayer As IGxLayer
Dim pGxFile As IGxFile
Set pGxLayer = New GxLayer
Set pGxFile = pGxLayer
pGxFile.Path = "D:\work\test.lyr"
' get new feature class.
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pWorkspace As IFeatureWorkspace
Set pWorkspace = pWorkspaceFactory.OpenFromFile("d:\work", 0)
Dim pNewFeatClass As IFeatureClass
Set pNewFeatClass = pWorkspace.OpenFeatureClass("wells")
' change lyr file's feature class.
Dim pLayer As ILayer
Set pLayer = pGxLayer.Layer
Dim pFeatLayer As IFeatureLayer
Set pFeatLayer = pLayer
Set pFeatLayer.FeatureClass = pNewFeatClass
Set pGxLayer.Layer = pFeatLayer
End Sub
Get help from ArcGIS experts
Download the Esri Support App