Thanks both,
The connecting code is the following:
-------------------------------------------------------------------------
Code:
Public Sub ConnectExcel(X As Variant)
' X is only used to suppress this routine from the Run Macro Dialog
' Determine the versions Excel that we have available on this machine,
' and set the public variables accordingly
On Error Resume Next
ExcelVer = 0
' Try to find a version of Excel 10
' Attempt to activate an existing instance
Err.Clear
Set ExcelServer = GetObject(, "Excel.Application.10")
If Err.Number = 0 Then
ExcelVer = 10
GoTo FINISH
End If
' Create a new instance
Err.Clear
Set ExcelServer = CreateObject("Excel.Application.10")
If Err.Number = 0 Then
ExcelVer = 10
GoTo FINISH
End If
FINISH:
' If we don't have Excel installed, refuse to run
If (ExcelVer = 0) Then
MsgBox "ExcelXP skal være installeret for at bruge denne metode", vbCritical, conDemoName
ExelLink.Hide
End If
ExcelServer.WindowState = -4140 ' xlMinimized
ExcelServer.Visible = True ' Make it visible
End Sub
-----------------------------------------------------------------------------
In between objWorksheet and objWorkbook is set:
Code:
Set objWorkBook = ExcelServer.Workbooks.Open("Path")
Set objWorksheet = objWorkBook.Sheets(1),
where Path is the path to my Excel workbook
The disconnecting code:
------------------------------------------------------------------------------
.......
Code:
Set objWorksheet = Nothing
Set objWorkBook = Nothing
Set ExcelServer = Nothing
' close Excel
Dim excelApp As Excel.Application
On Error Resume Next
Err.Clear
Set excelApp = GetObject(, "Excel.Application.10")
If Err <> 0 Then
Err.Clear
MsgBox "Excel er ikke aktiv.", vbExclamation
Else
excelApp.DisplayAlerts = False
excelApp.Quit
End If
Exit Sub
_______________________________________________
To mtuersley:
Excellink says about the code:
' Some of this code is not the way we would ordinarily
' write it within Excel. However, we need to work around
' a pair of Excel anomalies. If we refer to the Range
' object, or use the With/End With construction, it turns
' out that Excel does not remove itself from RAM when closed.
I will study your article
Thanks again
Claus
[ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]