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

ArcGIS Desktop Discussion Forums

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

Loop through all MXDs in a directory   Loring Tupper Jan 14, 2005
Re: Loop through all MXDs in a directory   Miles Hitchen Jan 21, 2005
Re: Loop through all MXDs in a directory   Kirk Kuykendall Jan 21, 2005
Re: Loop through all MXDs in a directory   Jason Thomas Nov 15, 2005
Re: Loop through all MXDs in a directory   Anika Meyer Aug 13, 2009
Re: Loop through all MXDs in a directory   James Stephenson Feb 03, 2005
Re: Loop through all MXDs in a directory   Loring Tupper Feb 07, 2005
Re: Loop through all MXDs in a directory   Kirk Kuykendall Feb 07, 2005
Re: Loop through all MXDs in a directory   Chris Duncan Oct 24, 2005
Re: Loop through all MXDs in a directory   Michael Gaud Jun 26, 2006
Re: Loop through all MXDs in a directory   Chris Duncan Jun 26, 2006
Re: Loop through all MXDs in a directory   Michael Gaud Jul 01, 2006
Re: Loop through all MXDs in a directory   Michael Gaud Jul 03, 2006
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Loop through all MXDs in a directory 
Author Loring Tupper 
Date Jan 14, 2005 
Message Does anyone have ny code that would loop through all MXD files in a directory? I want to start from an MXD file, click a button and loop hrough all of the MXDs in the current directory, running a sub-routine on each one. The sub-routine is to check for the existence of a particular layer and replace its source. I have the sub-routine but I need to run it on a lot of files and was hoping to find a way to loop through all of the files.

I did see the code that Kirk Kuykendall supplied for exporting PDFs from all MXDs in a directory but I can't seem to figure out how to get the looping through all MXDs portion to work.

Any help would be appreciated. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Miles Hitchen 
Date Jan 21, 2005 
Message Not sure if you're still having problems with this, but here's some code that will do what you want. I created it in ArcCatalog, but can provide code for ArcMap if you like.

The reason for storing the references to the mxd objects is because the VBA command Dir() tends to reset itself if there are any calls to other subs after the initial Dir(...) call.

Hope this helps,
Miles.
 
 
Private Sub ListMXDs()
Dim pApp As IGxApplication
Dim pEnumGxObj As IEnumGxObject
Dim pGxObj As IGxObject
Dim objCat As String
Dim folderPath As String
Dim pMxd(1000) As IGxObject
Dim mxdCnt As Integer
Dim nextMxd As String
Dim pObj As Variant

    Set pApp = Application
    Set pGxObj = pApp.SelectedObject
    objCat = LCase(pGxObj.Category)
    
    While objCat <> "folder" And objCat <> "folder connection" And objCat <> "arccatalog catalog"
        Set pGxObj = pGxObj.Parent
        objCat = LCase(pGxObj.Category)
    Wend
    
    If objCat <> "folder" And objCat <> "folder connection" Then
        MsgBox "No folder found", vbCritical
        Exit Sub
    End If
    
    folderPath = pGxObj.FullName
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    
    Dim objCnt As Long
    mxdCnt = 0
    nextMxd = Dir(folderPath + "*.mxd")
    While nextMxd <> ""
        Set pObj = pApp.Catalog.GetObjectFromFullName(folderPath + nextMxd, objCnt)
        If TypeOf pObj Is IEnumGxObject Then
            Set pEnumGxObj = pObj
            Set pMxd(mxdCnt) = pEnumGxObj.Next
        Else
            Set pMxd(mxdCnt) = pObj
        End If
        Debug.Print pMxd(mxdCnt).FullName
        mxdCnt = mxdCnt + 1
        nextMxd = Dir()
    Wend
    
End Sub
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Kirk Kuykendall 
Date Jan 21, 2005 
Message see below

kkeywords recursion 
 
Option Explicit
Sub Test()
    ListMxdsRecursively "E:\projects\forums"
End Sub

Sub ListMxdsRecursively(sFolder As String)
    ' use the filesystem object in
    ' Microsoft scripting runtime library
    Dim pFSO As New Scripting.FileSystemObject
    If pFSO.FolderExists(sFolder) Then
        Dim file As Scripting.file
        For Each file In pFSO.GetFolder(sFolder).Files
            If UCase(Right(file.Path, 4)) = ".MXD" Then
                DoSomethingWithMXD file.Path
            End If
        Next file
        
        ' recurse through subfolders
        Dim folder As Scripting.folder
        For Each folder In pFSO.GetFolder(sFolder).SubFolders
            ListMxdsRecursively folder.Path
        Next folder
    End If
End Sub

Sub DoSomethingWithMXD(sMXDFile As String)
    Debug.Print "fixing " & sMXDFile
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: Loop through all MXDs in a directory 
Author Jason Thomas 
Date Nov 15, 2005 
Message Kirk,

Thanks for the code. Just what I needed for a project.

-jason- 
  Jason Thomas
Provost & Pritchard Engineering, Group, Inc.
Fresno, CA 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Anika Meyer 
Date Aug 13, 2009 
Message Dear Kirk:
It's just a long time ago but perhaps you may answer my question.
When I try to run the test-procedure, I get the message "Run-time error '5': Invalid procedure or argument".
I work with ArcMap 9.2.
Do you have any idea, what may happens?
Thank you very much in advance.
Anika 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author James Stephenson 
Date Feb 03, 2005 
Message I am having the same problem. I have some code that exports the current open mxd to pdf. I need to be able to loop thru all the mxd's to export them to pdf. I am using sMXDFile = (Dir(sDir, "*.mxd", vbNormal) to build a list for my Do While loop. I am using "pMxDoc" in my code for the pdf export to express the document to export. I am unable to get pMxDoc = sMXDFile. I have no formal training with VB. I have searched thru the ESRI website and used the VB6 help and have read the VB digital books on cd, but I have been unable to come up with a solution. Thank you. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Loring Tupper 
Date Feb 07, 2005 
Message I tried Kirks suggested code but was immediately given an error message - "User defined type not defined."
This comes from the line:

Dim pFSO As New Scripting.FileSystemObject

When I checkerd this line out I found that I don't have a type called "Scripting.FileSystemObject" Perhaps I should explain that I am running this batch from within ArcMap. Is there a way of doing this from this using VBA from ArcMap?

Also I would like to use the current MXDs directory as opposed to specifying a directory in the code, how would I go about doing this?

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Kirk Kuykendall 
Date Feb 07, 2005 
Message In VBA, go into Tools>References and check "Microsoft Scripting Runtime". 
  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: Loop through all MXDs in a directory 
Author Chris Duncan 
Date Oct 24, 2005 
Message But what if the thing you want to do while looping is
to modify the MXD and save it with a new name?
Calling the Application's OpenDocument and
SaveAsDocument routines don't seem to work (opening
the next MXD seems to terminate the original
invocation of the looping subroutine, which I've
placed in Normal.mxt).

I've used code supplied by Kirk and others to successfully
batch-export image files for all MXDs in
a directory, but I've had no luck trying to use
similar schemes to batch-modify-and-save the MXDs
themselves. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Michael Gaud 
Date Jun 26, 2006 
Message Chris,

Did you ever figure out a solution to your problem?

I have a similar problem: I'm trying to loop through all the MXD files in a directory, adding a layer to each one. I'm using the code that Kirk Kuykendall posted here 1/21/05, substituting my own subroutine for his "DoSomethingWithMXD" subroutine. My subroutine starts with Application.OpenDocument, but that's where my code bombs, apparently because the VBA window closes.

I'm using version 9.1. I'm running the code from Normal.mxt, and my subroutines are public, not private. Kirk's subroutine "ListMxdsRecursively" seems to work correctly.

Thanks to anyone who can help with this problem.

-- Michael 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Chris Duncan 
Date Jun 26, 2006 
Message Hi Michael,

No, I never solved that one. I had the same problem with 9.0 and 9.1. Since first posting, I've had similar problems doing things from VBA for Excel - you can't do certain things with the document the code lives in (close, save as...). I have worked around the problem in Excel by saving a copy (ActiveWorkbook.SaveCopyAs) - haven't gone back to look if there's a similar functionality in ArcGIS/AO that could solve some problems of this nature.

Chris 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Michael Gaud 
Date Jul 01, 2006 
Message I finally came up with a solution. I saved the VBA code as a VBA module associated with a Word document, and ran it from there. That's my inelegant workaround for the problem of Arc closing itself and shutting down VBA -- the Word document stays open, so VBA doesn't shut down. I just had to make sure that the references to the ESRI Object Libraries were all included, in addition to Microsoft Scripting Runtime (see Kirk Kuykendall's post here Feb. 7, 2005).

I'm attaching the code. It's basically Kirk's code from Jan. 21, 2005. My modifications, which are cobbled together from various other sources plus trial and error, allow the code to open, save, and close the MXD from Word. I'm sure it's not the most efficient or pretty code (I'm only starting to learn VBA and ArcObjects), but it works. 
 
Option Explicit
Private m_pDoc As IDocument
Private m_pApp As IApplication

Public Sub Test()
    ListMxdsRecursively "C:\ArcStuff\ArcTest\LoopTest"
End Sub

Public Sub ListMxdsRecursively(sFolder As String)
    ' use the filesystem object in
    ' Microsoft scripting runtime library
    Dim pFSO As New Scripting.FileSystemObject
    If pFSO.FolderExists(sFolder) Then
        Dim file As Scripting.file
        For Each file In pFSO.GetFolder(sFolder).Files
            If UCase(Right(file.Path, 4)) = ".MXD" Then
                DoSomethingWithMXD file.Path
            End If
        Next file
        
        ' recurse through subfolders
        Dim folder As Scripting.folder
        For Each folder In pFSO.GetFolder(sFolder).SubFolders
            ListMxdsRecursively folder.Path
        Next folder
    End If
End Sub


Public Sub DoSomethingWithMXD(sMXDFile As String)

    Set m_pDoc = New MxDocument
    Set m_pApp = m_pDoc.Parent
    m_pApp.OpenDocument sMXDFile
    m_pApp.Visible = True

'-------------------------
' code that does stuff to MXD goes here
'   (hint: when setting the IMxDocument variable,
'    don't set it to "ThisDocument", but rather "m_pDoc":
'
'Dim pMxDoc As IMxDocument
'Set pMxDoc = m_pDoc
'
'-------------------------

    m_pApp.SaveDocument
    m_pApp.Shutdown
    Set m_pDoc = Nothing
    Set m_pApp = Nothing

End Sub
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Loop through all MXDs in a directory 
Author Michael Gaud 
Date Jul 03, 2006 
Message -- ADDENDUM -- Although the code I posted two days ago worked well on a test case, the code did not work on the actual files I needed to modify. The test case was a folder with a few small and simple MXD files with features from only one data source, and no additional text, north arrows, etc. The files I actually needed to edit were much more complex, had multiple data sources, text, north arrows, scale bars, etc. etc. Still, I canít think of why the code worked on the first group of files but not the second.

When I run the code on the second group of files, the first file in the folder opens as "Untitled," and then a "Save As..." window opens. The code grinds to a halt and I get an error -- the errors I've gotten are both "The server threw an exception" and "Method 'SaveDocument' of object 'IApplication' failed".

I've done a little troubleshooting to learn the following:
- neither set of files is read-only
- the problem is not a file-name or file-path-name issue
- I can't find any differences in their map properties (File --> Map properties...)
- the problem is not related to the location of the VBA code (in the Word document) relative to the files I'm trying to edit
- the problem is not related to the code that modifies the MXD (rather, it's the looping code I posted that doesn't work)

I'm completely mystified. I'd be very grateful if someone could help me figure out why my two sets of files behave differently using the same code.

-- Michael