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