Comrades who use mxd must have encountered the following problems:
An mxd is copied to another machine, or the SDE server is changed. After the mxd is turned on, there is an annoying red exclamation point in front of all layers. The connection is lost. What should we do at this time?
Method:
1. Manual Modification
1) disconnect the network and open mxd. The network is disconnected to speed up mxd activation;
2) connect to the network
3) on a layer, right-click and choose data> repair data source and select the SDE server or another data source type. Mxd is automatically repaired.
Disadvantages:
If we have multiple valid data sources, but want to switch the data source, you will find that the layer automatically repaired still uses the old data source. It seems that you need to modify layers one by one.
2,CodeModify
You can copy the following code to the VBA environment, modify the connection string, and execute it. You can instantly modify the data source. Isn't that great? Of course, it can also be made an independentProgramUsed to repair 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
'Get Layers
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
'Modify the connection string
Pwsname. pathname = "C: \ Documents ents and Settings \ Administrator \ Application Data \ ESRI \ arccatalog \ connection to localhost (DLG). Sde"
'Reconnect
Pdatalayer. Connect pdatasetname
'Use the following code to display the connection information
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 if
Debug. 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