How can I make my automation code hook onto an open application?

K

Kristian

Hello,
I've written some code that performs some automation in
Excel. My code creates a new workbook and enters some
stuff from my Word document.
Problem:
I set an object variable = NEW Excel.Application
Hence, although Excel is running the code opens
a new copy of the Excel application into memory.

Anyone got a nice solution to this?

Thanks in advance,
Kristian
 
J

Jezebel

Dim pxlApp as Excel.Application

Set xlApp = GetObject(, "Excel.Application")

This will return an existing instance of Excel if there is one. It will
raise an error and return nothing if Excel is not running. You can trap the
error (or simply ignore it and check whether xlApp is nothing) and use

If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
end if
 
Top