Test for open excel workbook and bring forward


Thread Starter

Dan Niblett


I need to test for an open excel workbook from RSView32. I need to open it and bring it forward if it is not open; if it is open, I need to simply bring it forward. The appstart opens a new copy of the workbook every time. The appactivate command will not bring it forward, just activates the task bar and highlights the excel button, but that's all.

I am close with this VBA code; it does open the workbook if not open and brings it forward. It does not allow more than one copy to be open.
However, it will not bring the workbook forward if it is already open. It will activate the task bar and highlight the excel button, just like the appactivate command,but that's all. I tested it in MSWord also with the same results.

Sub OpenandorActivateandBringForward()
Dim ExcelFile As Object
'dim ExcelFile as excel.workbook 'early binding
Set ExcelFile = GetObject("C:\Documents and Settings\udn4973\My Documents\Projects\plantsysdemo\temps.xls")
ExcelFile.Parent.Visible = True
ExcelFile.Parent.UserControl = True
ExcelFile.Windows(1).Visible = True
End Sub

Anyone have any previous luck with this?


DAVCO Automation

Are you using Win2k... there is a bug in win2k that will not allow items that are not in the forground and have focus to activate or something like that... it is a registry hack and I have had to use it to bring RSLogix... it would open but not come forward and if open flash on taskbar....

It is something like foregroundpriority time... found on knowledge base at Rockwell... if you contact me offlist, I can find the hack as I have it documented in my mass filing system... (somewhere)...


Use [email protected]... please no spam.

In reply to Dan:
Your routine works if the file is already open, either in the same Excel session or in a separate one.
It doesn't open it in a separate version of Excel if it is NOT open anywhere, it opens it in the same one.
This routine will open it in a separate session:

Dim xlApp As Excel.Application
Dim ExcelFile As Excel.Workbook
Set xlApp = New Excel.Application
Set ExcelFile = xlApp.Workbooks.Open("C:\Excel\User\MyFile.xls")
xlApp.Parent.Visible = True
xlApp.Parent.UserControl = True
xlApp.Windows(1).Visible = True

...but opens a new one EVERY time (read only after the first one of course).

I am trying to find how to check if it is open already in a separate session, so I can do an IF-THEN-ELSE on it, because I wish to have MyFile.xls opened in a separate session (only once) to prevent OnKey conflicts.

P.S. To get the focus on it, you can use:
ExcelFile.Parent.WindowState = xlMaximized