Using Application.OpenDocument without interrupting the macro

2971
2
07-28-2010 12:23 PM
DominicComtois
New Contributor
Hi there,

I need to perform a series of actions on all the .mxd documents present in a specific directory. So I have something like this:

For Each filename in directoryListing
    Application.OpenDocument (filename)
    Action1
    Action2
    ...
Next filename


Problem is, when the macro reaches the OpenDocument command, the document is duly opened but the macro is interrupted.

Any help mostly welcome.
0 Kudos
2 Replies
DominicComtois
New Contributor
Found my solution in this example:http://edndoc.esri.com/arcobjects/8.3/Samples/Application%20Framework/Automation%20%28Start%20ArcMap...

The key is this, in case anyone is looking for the same thing:

Set m_pDoc = New MxDocument
Set m_pApp = m_pDoc.Parent
m_pApp.OpenDocument ("filePath")
(additional code here)
m_pApp.shutdown

Good luck


** Edit: I THOUGHT this would do the trick, but it doesn't, since the macro keeps sending instructions to the main document, i.e. the one that was already open when the macro was launched.

From here, any help is mostly welcome.
0 Kudos
BrianBottoms
New Contributor
Hi,

I approached a similar problem from a slightly different direction.  I needed to iterate MXDs and with each one change scales, text, etc. as well as print.  At ESRI�??s suggestion, I used the GetDeskTopWindow function in order to establish the ActiveView ( ESRI Help -  �??When working with the IActiveView interface on a MapDocument object, you should always first call IActiveView::Activate() in order to properly initialize the display of the PageLayout or Map object�?�).  Essentially, I am using an empty ArcMap document, attaching to its activeview to fully initialize the MapDocument object and from there I am able to open one MXD after another and more importantly manipulate elements in the MXD as well as save.  It�??s a little awkward conceptually, but works.  I just tested it in ArcGIS 10 and it still works smoothly.

Brian

ArcGIS 10 demo app:

Option Strict On
Imports ESRI.ArcGIS.Carto
Imports System.IO
Public Class QueryMXDs
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button
    Private Declare Function GetDesktopWindow Lib "user32" () As Integer
    Public Sub New()

    End Sub

    Protected Overrides Sub OnClick()

        Dim myDirPath As String = "C:\Temp" '****CHANGE******

        Dim al As ArrayList = New ArrayList(Directory.GetFiles(myDirPath, "*.mxd"))
        Dim pMapDocument = New MapDocumentClass
        Dim pActiveview As IActiveView
        Dim pPageLayout As IPageLayout
        Dim pMap As IMap
        Dim o As Object


        For Each o In al
            pMapDocument.Open(CStr(o))
            pPageLayout = pMapDocument.PageLayout
            pActiveview = CType(pPageLayout, IActiveView)
            pActiveview.Activate(GetDesktopWindow())
            pMap = pMapDocument.ActiveView.FocusMap

            If pMap.LayerCount > 0 Then
                Windows.Forms.MessageBox.Show(CStr(o) & "'s first layer:" & vbCrLf & pMap.Layer(0).Name)
            End If

            pMapDocument.Close()
        Next


        My.ArcMap.Application.CurrentTool = Nothing
    End Sub

    Protected Overrides Sub OnUpdate()
        Enabled = My.ArcMap.Application IsNot Nothing
    End Sub

End Class
0 Kudos