Problem copying layer file (.lyr) to a different location

07-18-2011 09:14 PM
New Contributor
Hi there,

I have written some code which exports a feature class in the TOC to a shapefile and a layer file. However when I copy the output results to a different location it seems the layer file's data source is still looking at the original location. The shapefile changes its data source to the new location but the layer file doesn't.

What I am looking to do is run my code, get the shapefile and layer file outputs and pass them to work mates. There are hundreds of outputs so I would like to automate this process. But as it stands my work mates would have to manually repair the data source for each and every broken layer file I give them.

I suspect it might be an issue regarding relative path names or resetting the data source of the layer file but I'm just not sure how to go about either option as I am new to VBA and learning as I go.

My code so far is ...

'Create layer file
Dim pGxLayer As IGxLayer
Dim pGxfile As IGxFile
Set pGxLayer = New GxLayer
Set pGxfile = pGxLayer
pGxfile.Path = "D:\GEOLOGY_REGIONAL_JULY2011\BOWEN BASIN\ESRI Shapefile\MyLayer.lyr"
Set pGxLayer.layer = pMap.layer(0)

Set pGxfile = Nothing
Set pGxLayer = Nothing

'Set data source of layer file
Set pGxLayer = New GxLayer
Set pGxfile = pGxLayer   

Dim pDLayer As IDataLayer2
Set pDLayer = pGxLayer.Layer
Dim pDSName As IDatasetName
Set pDSName = pDLayer.DataSourceName
Debug.Print "old path : " & pDSName.WorkspaceName.PathName
Dim pWSName As IWorkspaceName
Set pWSName = pDSName.WorkspaceName
pWSName.PathName = 'I'm not sure what to put here I've tried
Debug.Print "new path: " & pDSName.WorkspaceName.PathName
pDLayer.DataSourceName = pDSName
Set pGxLayer.Layer = pDLayer

For the new path name or to set the path name to relative I'm not sure what to put in ...


And neither of these works ...
Any help would be greatly appreciated. Thanks so much.

0 Kudos
3 Replies
Regular Contributor III
Have you thought about just creating a layer package or a map package if you're using ArcGIS 10?
0 Kudos
New Contributor
Unfortunately I'm using 9.3.1. I see you can manually export to a layer package in 9.3.1. Do you know if you can use vba or python to export out as a layer package?

0 Kudos
Regular Contributor III
I don't think there is any direct support in the ArcObjects model but you should be able to execute the context menu's command item.  Just set the document's context item prior to executing it.

Sub CreateLayerPackage()
    Dim uid As New uid
    uid.Value = "{18DF94D9-0F8A-11D2-94B1-080009EEBECB}:23"
    Dim mxDoc As IMxDocument
    Set mxDoc = ThisDocument
    mxDoc.ContextItem = mxDoc.FocusMap.Layer(0)
    Dim commandItem As ICommandItem
    Set commandItem = ThisDocument.CommandBars.Find(uid)
End Sub
0 Kudos