PDA

View Full Version : Solved: Check if an Excel is open and if not open it from within word



RECrerar
12-18-2007, 09:33 AM
Hi,

Quick question to do exactly what the title says, how can I check if an instance of Excel is running and if not open excel from within word.

I can create the document using


Set exl = CreateObject("Excel.Application")

But am a bit stuck on how to check if it is already open. Thanks in advance

TonyJollans
12-18-2007, 10:51 AM
The GetObject Function will get a reference to an existing object and return an error if there is none. If you trap the error you can then use CreateObject when necessary.

RECrerar
12-19-2007, 02:01 AM
Hey thanks for that, that works great. I now have the code below that will check if excel is open and if not open it.


Option Explicit
Dim exl As Object
Sub OpenExcel()
If ExcelOpen("Excel.Application") = False Then
Set exl = CreateObject("Excel.Application")
End If
exl.Visible = True
End Sub



Function ExcelOpen(Nm As String) As Boolean

On Error GoTo exOpen
Set exl = GetObject(Nm)
ExcelOpen = True
Exit Function
exOpen:
ExcelOpen = False
End Function

I have a couple of add-in's in excel that create their own menu items, however when I open excel this way the menu items are not added, which kinda defeats the point of opening Excel automatically. Do you know why this is? I'm thinking that it must be something to do with the macro security as the Excel open procedure does not show the option box to enable macros.

If you/anyone else knows how to open Excel so the add-ins are active that would be great, however if it's going to be very complex I can always just produce a message box telling the user they have to do something themselves!

TonyJollans
12-19-2007, 03:23 AM
It is true in Word, but I'm not quite 100% sure about Excel off the top of my head, that auto-macros are suppressed when the application is started via automation. If you want anything to run you have to do it explicitly.

RECrerar
12-19-2007, 05:37 AM
That makes sense,

Shame though, it would have been a nice feature. Still I shall consider this a learning experience and move on. Thanks for the help.