Is This Content Helpful?
We're glad to know this article was helpful.
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.
In the following example, the TEST.LYR file will be modified to point to a different shapefile called WELLS, located in the C:\WORK directory.
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.
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