使用MXD的同志们肯定遇到这样的问题:
一个MXD复制到其它机器上,或者SDE服务器发生了变化,MXD打开后所有图层前都有一个讨厌的红色感叹号。连接丢失了。这个时候怎么办呢?
方法:
1、手工修改法
1)断掉网络,打开MXD。断掉网络是为了加快打开MXD的速度;
2)连上网络
3)在一个图层上,点击鼠标右键,选择【Data】-【Repair Data Source】,选择正确的SDE服务器,或者其它类型数据源。MXD会自动修复。
缺点:
如果我们有多个有效的数据源,只是想切换数据源,修复的时候,你会发现,自动修复的图层仍然采用了之前旧的数据源。似乎要逐个图层去修改。
2、代码修改
可以把下面代码Copy到VBA环境,修改连接串,执行,可以在瞬间完成数据源的修改,是不是很爽?当然也可以做成一个独立的程序,用来修复MXD。
Private Sub IDataLayerExample()
Dim pMxdoc As IMxDocument
Dim pApp As IApplication
Set pApp = Application
Set pMxdoc = pApp.Document
Dim pLayer As ILayer
Dim pDataLayer As IDataLayer
Dim pDatasetName As IDatasetName
Dim pWSName As IWorkspaceName
Dim sFDS As String
Dim pFCName As IFeatureClassName
Dim i As Integer
For i = 0 To pMxdoc.FocusMap.LayerCount - 1
'获取图层
Set pLayer = pMxdoc.FocusMap.Layer(i)
If TypeOf pLayer Is IDataLayer Then
sFDS = ""
Set pDataLayer = pMxdoc.FocusMap.Layer(i)
Set pDatasetName = pDataLayer.DataSourceName
Set pWSName = pDatasetName.WorkspaceName
'修改连接串
pWSName.PathName = "C:\Documents and Settings\Administrator\Application Data\ESRI\ArcCatalog\Connection to localhost(dlg).sde"
'重新连接
pDataLayer.Connect pDatasetName
'以下代码用来显示连接信息
Set pDatasetName.WorkspaceName = pWSName
If TypeOf pDatasetName Is IFeatureClassName Then
Set pFCName = pDatasetName
If Not pFCName.FeatureDatasetName Is Nothing Then
sFDS = pFCName.FeatureDatasetName.Name
End If
End IfDebug.Print "(" + Format(i) + ") Path: " + pWSName.PathName
Debug.Print "Feature Dataset: " + sFDS
Debug.Print "Feature Class/Dataset: " + pDatasetName.Name
Debug.Print
End If
Next i
End Sub