Keeping Excel on Top of Word


Q: How can I work in Word while keeping Excel on top? 

A. You can achieve that by using the SetWindowsPos API procedure

1. Add a standard module to Normal and put the following code there

 Option Explicit

'Declare the two API calls we have to make
Public Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function GetForegroundWindow _
Lib "user32" () As Long

'Declare the four constants required in the API calls
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOACTIVATE = &H10

'Declare a variable to be used as window handle to Excel
Public hWnd As Long

Sub OpenExcel()
  'This macro is called to start Excel

  'Start Excel
  Shell "D:\Program\Microsoft Office\Office\Excel.exe", _
  vbNormalFocus

  'Give Excel time to start
  DoEvents

  'Find the handle to Excel
  hWnd = GetForegroundWindow

  'Use SetWindowPos to move Excel topmost

  SetWindowPos hWnd, HWND_TOPMOST, 200, 200, 500, 500, 0
End Sub

2.  Put the following code in the ThisDocument module in Normal

Option Explicit

'Declare an Application-type variable
Public WithEvents appRef As Application

Sub AutoExec()
  'Set appRef to refer to the Word Application object
  Set appRef = Application
End Sub

Private Sub appRef_WindowActivate(ByVal Doc As Document, _
  ByVal Wn As Window)
  'Excel will not remain topmost, once we move the focus
  'to the Word document. Thus we'll have to move Excel
  'topmost (but unactivated) again


  SetWindowPos hWnd, HWND_TOPMOST, 200, 200, 500, 500, _
    SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATE
End Sub

3.  Close Word, saving Normal if you are asked to.

4.  Start Word again and start Excel using the OpenExcel macro