Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling Excel Instances

Status
Not open for further replies.

hatman88

Programmer
Aug 22, 2007
16
US
Hello, I have a macro that takes data from an Excel sheet, performs certain tasks in Extra, then writes onto the sheet. I can get that working normally, but I would like it so that if the Excel sheet is already open, it writes onto that particular sheet. But if it is not opened, then the macro makes its own instance of Excel.

I can only get it so that it always makes its own instance. Any help? Thanks.
 
Sub Main
Dim oXL As Object
Dim oWB As Object
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
Set oXL = CreateObject("Excel.Application")
Set oWB = oXL.Workbooks.Add
oXL.Visible = True
Else
Set oWB = oXL.Workbooks(1)
End If
On Error Goto 0
End Sub
 
Use GetObject to get an existing object. Use CreateObject to create a new instance of an object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top