Check if Excel Application is already open

  • Thread starter Thread starter R.W.
  • Start date Start date
R

R.W.

In one of my projects I create an Excel Workbook and add
a new Excel workbook with this code:

Set xlsObject = CreateObject("Excel.Application")
Set xlsWorkbook = xlsObject.Workbooks.Add()

But when the user previously has opened MS Excel himself,
this causes problems. So I need a methode to check if the
user has opened Excel already. If not I can use previous
code otherwise I need a way to assign the open Excel
application to the variable xlsObject.

All positive suggestions welcome, thanks

R.W.
 
In the references, set a reference to Excel

Then use the following code:

On Error Resume Next
Set xlsObject = GetObject(, "Excel.Application")
Err.Clear
If xlsObject Is Nothing Then
Set xlsObject = CreateObject("Excel.Application")
End If
On Error GoTo Errorhandler
Set xlsWorkbook = xlsObject.Workbooks.Add()

This will look to find an instance of Excel and if there is one use it. If
there is not, an error is generated (hence the On Error Resume Next) and
then a new instance of Excel is created.

Dave
 
...
In one of my projects I create an Excel Workbook and add
a new Excel workbook with this code:

Set xlsObject = CreateObject("Excel.Application")
Set xlsWorkbook = xlsObject.Workbooks.Add()

But when the user previously has opened MS Excel himself,
this causes problems. So I need a methode to check if the
user has opened Excel already.

Try:

On Error Resume Next
Set xlsObject = GetObject(, "Excel.Application")
On Error GoTo 0
If xlsObject Is Nothing Then
Set xlsObject = CreateObject("Excel.Application")
End If

Jamie.

--
 
Back
Top