0

How would you focus your ArcMAP application window using VBA?

I have a script that runs and selects polygons and zooms to them, but I would like ArcMap to focus and come to the front at that point.

Suggestions?

Dim pDoc As IDocument
Dim pApp As IApplication
Dim pAppROT As IAppROT
Set pAppROT = New AppROT
If pAppROT.Count > 0 Then
Dim p As Integer
Dim x As Integer
p = 0
x = 0
Do While p < pAppROT.Count
If TypeOf pAppROT.Item(p) Is IMxApplication And (pAppROT.Item(p).Document.Title Like "Report.mxd") Then
Set pDoc = pAppROT.Item(p).Document
Set pApp = pDoc.Parent
x = 1
pAppROT.Item(p).Visible = True
End Sub
Set focus = pAppROT.Item(p)
Exit Do
Else
End If
p = p + 1
Loop
End If
If x = 0 Then
 Set pDoc = New MxDocument
 Set pApp = pDoc.Parent
 pApp.OpenDocument ("c:\Report.mxd")
 pApp.Visible = True
End If'
Andre Silva
10.5k12 gold badges57 silver badges109 bronze badges
asked Apr 5, 2012 at 14:29

1 Answer 1

2

You can call the Win32 API SetForegroundWindow function as follows:

Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Sub ViewArcMapWindow()
 SetForegroundWindow Application.hwnd
End Sub

However, if you run this directly from the VBA IDE, it may not work as desired since VBA may activate the IDE window when the script finishes.

answered Apr 5, 2012 at 15:09
5
  • How might i incorporate this into my code? I can't seem to figure out if i call the AppROT funciton or something different. thanks Commented Apr 5, 2012 at 15:23
  • Just cast your IMxApplication to IApplication and pass its window handle (.hwnd property) to the SetForegroundWindow function... Commented Apr 5, 2012 at 16:01
  • Inside what application do you run the VBA snippet? Commented Apr 5, 2012 at 16:02
  • I'm running it from a Access 2002 dbase to open arcmap and zoom to a specific polygon. The whole script works just fine, its bringing Arcmap to the front of the display thats catching me. I'll try your solution Commented Apr 5, 2012 at 18:23
  • I guess what I am confused mostly about is the syntax and how i might apply it to my code. I have dimensionalized IMxApplications as the IApplication but it still seems to hang/I am having troubles in solving the hwnd piece. Commented Apr 5, 2012 at 18:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.