I have problem creating new instance of excel 2007 using VBA (from Access 2002).
At first I misunderstood the problem and it is more complicated than I thought.
Set myXL = CreateObject("Excel.Application")
Creates new processes (I thought it didn't), but still strange thing is happening. I use OLE to store Excel files in SQL Server databse.
This is the function that bounds excel file to a form:
Public Function OpenExcelObjFrame() As BoundObjectFrame
Dim myXL As Object
Set myXL = CreateObject("Excel.Application") '#1
Set gTempWorkbook = myXL.Workbooks.Add
myXL.Visible = True '#2
DoCmd.OpenForm "Excel_OBJ"
Forms("Excel_OBJ").Visible = False
Forms("Excel_OBJ").RecordSource = "SELECT Excel_File FROM Excel_File_Obj;"
Set OpenExcelObjFrame = Forms("Excel_OBJ").Excel_File
End Function
The context:
Dim tExcelObjFrame As BoundObjectFrame
Set tExcelObjFrame = OpenExcelObjFrame()
tExcelObjFrame.Verb = acOLEVerbOpen
tExcelObjFrame.Action = acOLEActivate '#3
ad 1 2nd process excel.exe starts
ad 2 Added this line to check what is happening to the 2nd process
ad 3 OLE bounds the file to existing excel instance, the 2nd process is killed here :/
So the problem is:
Why the tExcelObjFrame.Action = acOLEActivate kills the 2nd process
How to force Object Frame to activate excel file in the 2nd process
EDIT
I've found partial explanation here (with no clear solution):
http://www.xtremevbtalk.com/showthread.php?t=292170
- If no instance of Excel is running, OLE creates an instance of Excel when you access the object, either directly or through programmation.
- If an instance of Excel is already running, then this is the instance that is used by the OLE object.
The problem is that OLE object chooses wrong excel instance. Question is if there is a way to point OLE to a specific instance.
Same problem described here (no solution given):
Update:
I've been trying your existing code on my workstation and the:
Set myXL = CreateObject("Excel.Application")
seems to works as you're requesting.
I've had one process open, then I run that line and it creates another process.
I've been investigating a little further and it turns out that this is a bit of a quirk of Excel 2007.
Everything I've been looking at leads me back to windows file type (scroll right to the bottom) or registry hacks, which are basically changing the same thing in this situation.
I know that in VBA you can play with registry keys with CreateNewKey and SetKeyValue, but I'm not sure that you'll want to do that.