You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

Programmaticaly convert many mxd's to relat...   Aleek Datta May 30, 2002
Re: Programmaticaly convert many mxd's to r...   Kirk Kuykendall May 30, 2002
Re: Programmaticaly convert many mxd's to r...   Kirk Kuykendall May 30, 2002
Re: Programmaticaly convert many mxd's to r...   Aleek Datta May 31, 2002
Re: Programmaticaly convert many mxd's to r...   Duc Pham Oct 16, 2002
Re: Programmaticaly convert many mxd's to r...   Kirk Kuykendall Oct 16, 2002
Re: Programmaticaly convert many mxd's to r...   Andrew Zimba Nov 09, 2005
Re: Programmaticaly convert many mxd's to r...   Guangming HE Mar 20, 2005
Re: Programmaticaly convert many mxd's to r...   Kirk Kuykendall Mar 16, 2007
Re: Programmaticaly convert many mxd's to r...   Ajit Marathe Apr 26, 2010
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Programmaticaly convert many mxd's to relative path 
Author Aleek Datta 
Date May 30, 2002 
Message I'm a relatively new at this, so please be patient. I have a set of mxd files, currently saved using absolute path, that i have to move to a different directory (easily done). However, once i get them all to the new location, I want to save each mxd using relative paths (for distribution). Is there any quick way to loop through the set of mxd's and save them using relative paths...the only way that I know is to use open each mxd using Application.OpenDoc, then change the .RelativePath = True, and then savedoc. However, it seems that each time i do this, a new instance of ArcMap opens, and it's just taking a long time (i need to do this for 500+ mxd's).

Can anyone PLEASE offer some advice?
Thanks in advance,
Aleek 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Kirk Kuykendall 
Date May 30, 2002 
Message try this 
 
Option Explicit
Private WithEvents DocEvents As DocumentEvents
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private m_bWakeupFlag As Boolean
Sub Test()
    ' loop through all your files here, might need to sleep
    ' within the loop
    ChangeRelPath "D:\arcgis\sampledata\junk.mxd"
End Sub

Sub ChangeRelPath(sFilename As String)
    Dim pDoc As IDocument
    Set pDoc = New MxDocument
    Set DocEvents = pDoc
    pDoc.Parent.OpenDocument sFilename
    Set pDoc = Nothing
    m_bWakeupFlag = False
    Do Until m_bWakeupFlag
        DoEvents
        Sleep 100
    Loop
    Debug.Print "waking up"
    Set DocEvents = Nothing
End Sub

Private Sub DocEvents_OpenDocument()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = DocEvents
    Debug.Print "opened"
    pMxDoc.RelativePaths = True
    Dim pDoc As IDocument
    Set pDoc = pMxDoc
    Debug.Print "saving " & pDoc.VBProject.FileName
    pDoc.Parent.SaveAsDocument pDoc.VBProject.FileName
    pDoc.Parent.Shutdown
    m_bWakeupFlag = True
End Sub
 
  Kirk Kuykendall
AmberGIS Programming Services & Sales
http://www.ambergis.com
Now Answering questions at:
http://gis.stackexchange.com/


 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Kirk Kuykendall 
Date May 30, 2002 
Message I've gone ahead and changed it to do files in a directory. 
 
Option Explicit
Private WithEvents DocEvents As DocumentEvents
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private m_bWakeupFlag As Boolean
Sub Test()
    ' loop through all your files here
    Dim sDir As String
    sDir = "f:\mxds\"
    Dim sFile As String
    sFile = Dir(sDir & "*.mxd", vbNormal)
    Do While sFile <> ""
        Debug.Print "fixing " & sDir & sFile
        ChangeRelPath sDir & sFile
        sFile = Dir
    Loop
    Debug.Print "done"
End Sub

Sub ChangeRelPath(sFilename As String)
    Dim pDoc As IDocument
    Set pDoc = New MxDocument
    Set DocEvents = pDoc
    pDoc.Parent.OpenDocument sFilename
    Set pDoc = Nothing
    m_bWakeupFlag = False
    Do Until m_bWakeupFlag
        DoEvents
        Sleep 100
    Loop
    Debug.Print "waking up"
End Sub

Private Sub DocEvents_OpenDocument()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = DocEvents
    Dim pDoc As IDocument
    Set pDoc = pMxDoc
    Debug.Print pDoc.VBProject.FileName & " opened"
    pMxDoc.RelativePaths = True
    Debug.Print "saving " & pDoc.VBProject.FileName
    pDoc.Parent.SaveAsDocument pDoc.VBProject.FileName
    Set DocEvents = Nothing
    pDoc.Parent.Shutdown
    DoEvents
    m_bWakeupFlag = True
End Sub
 
  Kirk Kuykendall
AmberGIS Programming Services & Sales
http://www.ambergis.com
Now Answering questions at:
http://gis.stackexchange.com/


 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Aleek Datta 
Date May 31, 2002 
Message Kirk,

The script works great...thanks a lot for you help, much appreciated...

Aleek
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Duc Pham 
Date Oct 16, 2002 
Message How can I use this script?. Where do I put it?

Thanks 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Kirk Kuykendall 
Date Oct 16, 2002 
Message Tools>Macros>VBAEditor ...
double click Thisdocument under Project in treeview,

cut & paste code, change the wildcarded path from F:\mxds to the directory where your mxds are, run the Test sub. 
  Kirk Kuykendall
AmberGIS Programming Services & Sales
http://www.ambergis.com
Now Answering questions at:
http://gis.stackexchange.com/


 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Andrew Zimba 
Date Nov 09, 2005 
Message Kirk,

This code is great! I am having trouble figuring out how to modify the code to search recursively through a set of subfolders of varying depths for mxd's to modify. For example, it would be great to be able to search through an entire projects drive and globally set all the mxd's in that whole drive to use relative paths.

I have looked at the ArcObjects Online search recursively sample, but cannot quite figure out how to put that code together with your code.

Can you help me out? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Guangming HE 
Date Mar 20, 2005 
Message Why my codes just work for 2 mxd documents and then stop?

Guangming
 
 
'link to open doc event to open a mxd doc
Private Sub OpenMXDDoc(sFilename As String)
    'On Error Resume Next
    
    Dim pDoc As IDocument
    Set pDoc = New MxDocument
    
    Set DocEvents = pDoc
    pDoc.Parent.OpenDocument sFilename
    Set pDoc = Nothing
    m_bWakeupFlag = False
    Do Until m_bWakeupFlag
        DoEvents 'pass the doc object to the function implementing the event
        Sleep 100
    Loop
    Set DocEvents = Nothing
    'Debug.Print "waking up"
End Sub

 
'called after openning a doc
Private Sub DocEvents_OpenDocument()
    'On Error Resume Next
    Dim pmxdoc As IMxDocument
    Set pmxdoc = DocEvents 'get the object (a mxd doc) from the event
    'Debug.Print pDoc.VBProject.FileName & " opened"
        'Change Layer Data Source of mxd document
        ChangeLayerDataSource pmxdoc
        'Change Relative path of mxd document
        ChangeMXDRelativePath pmxdoc
    
    Dim pDoc As IDocument
    Set pDoc = pmxdoc
    Debug.Print "  saving " & pDoc.VBProject.FileName
    pDoc.Parent.SaveAsDocument pDoc.VBProject.FileName
    
    Set DocEvents = Nothing
    pDoc.Parent.Shutdown
    DoEvents
    m_bWakeupFlag = True
End Sub
 
  Share your ideas with me! 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Kirk Kuykendall 
Date Mar 16, 2007 
Message IMapDocument seems to be a more reliable way to accomplish this (9.0+).

kkeywords imapdocument 
 
Option Explicit
Sub Test()
    SetAllRelative "C:\Program Files\ArcGIS\DeveloperKit\relpathtest"
End Sub

Sub SetAllRelative(sDir As String)
    
    ' recursively searches for mxds under a folder
    ' and subfolders and sets all of them to use
    ' relative paths.
    
    ' To compile go into Tools>References and check
    ' "Microsoft Scripting Runtime"
    Dim pFSO As Scripting.FileSystemObject
    Set pFSO = New Scripting.FileSystemObject
    If Not pFSO.FolderExists(sDir) Then
        MsgBox "folder not found"
        Exit Sub
    End If
    Dim pFile As Scripting.File
    For Each pFile In pFSO.GetFolder(sDir).Files
        If UCase(Right(pFile.Path, 3)) = "MXD" Then
            SetRelative pFile.Path
        End If
    Next pFile
    
    Dim pFolder As Scripting.Folder
    For Each pFolder In pFSO.GetFolder(sDir).SubFolders
        SetAllRelative pFolder.Path
    Next pFolder
End Sub


Sub SetRelative(sPath As String)
    Dim pMapDoc As IMapDocument
    Set pMapDoc = New MapDocument
    If Not pMapDoc.IsReadOnly(sPath) Then
        pMapDoc.Open sPath
        If Not pMapDoc.UsesRelativePaths Then
            Dim bHasThumbnail As Boolean
            bHasThumbnail = Not (pMapDoc.Thumbnail Is Nothing)
            Debug.Print "saving " & sPath
            pMapDoc.Save True, bHasThumbnail
        Else
            Debug.Print sPath & " already uses relative paths"
        End If
        pMapDoc.Close
    Else
        Debug.Print sPath & " is readonly"
    End If
    Set pMapDoc = Nothing
End Sub
 
  Kirk Kuykendall
AmberGIS Programming Services & Sales
http://www.ambergis.com
Now Answering questions at:
http://gis.stackexchange.com/


 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Programmaticaly convert many mxd's to relative path 
Author Ajit Marathe 
Date Apr 26, 2010 
Message Thanks for the code Kirk. Most of the time it works correctly however when there are arcgisonline services added as layers in ArcMap, the code hangs. I believe this is due to the use of the FileSystemObject. Any ideas?