View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default excel to autocad routine opens a new instance of excel that has no worksheet.

Mary,
Looks like GetObject is failing because there is no instance of Excel
running, so you are creating a new empty instance, even though that code is
commented out in your sample.
Also, whilst not the cause your problem, it would be better to use something
like "XLApp" instead of "excel" for the variable name.

Assuming ACAD VBA behaves the same as in Office, this line really means
nothing:
If Err<0 Then
because you have no "On Error Resume Next" before GetObject. So if there is
an error, it will not be handled.

So basically your is code is working (after you add "On Error Resume Next").
But because you need to work with the current XL file, you should exit your
code if GetObject fails, because you have nothing to work with.
Also, you do know that if you have more than 1 instance of Excel running,
GetObject will give access to 1 instance essentially at random. So you may
not get the reference to workbook you desire.

NickHK

wrote in message
oups.com...
Here is the code
Mary
----------
Dim rng As Range


Dim excel As Object
Dim acad As Object

-------- Excess code cut -------------
Private Sub CommandButton1_Click()

EXAMPLE_GETVARIABLE

Set excel = GetObject(, "Excel.Application")
If Err < 0 Then
Err.Clear
'Set excel = CreateObject("Excel.Application")
'If Err < 0 Then
'MsgBox "Could not load Excel.", vbExclamation
'End
'End If
End If
On Error GoTo 0

excel.Visible = True
mysheet = excel.ActiveWorkbook.ActiveSheet.Name
Set excelsheet = excel.ActiveWorkbook.Sheets(mysheet)
Set rng = excel.Application.InputBox(Prompt:="Select range", Type:=8)
begrow = rng(1).Row

--------------- Excess code cut -----------