VBA & Excel: "Object required" error

VBA & Excel: "Object required" error

Anonymous
Not applicable
3,787 Views
3 Replies
Message 1 of 4

VBA & Excel: "Object required" error

Anonymous
Not applicable

Hello All:

 

I would like to read an excel file from Autocad and use some values in my VBA program. But I have a problem and I don't manage to solve it on my own even though I did a lot of research.

If you look at my code below, the GetCellValue function does not work: I have the run time error 424  "object required". I do not understand this error because ExcelConnect works and finds my excel file.

Could you please help me? Any help would be greatly appreciated.

Thanks,

 

Here is my code:

 

Option Explicit
Dim excelapp As Object
Dim mspace As AcadModelSpace

 

Function ExcelConnect()
' This function connects excel with autocad

'On Error Resume Next
Set excelapp = GetObject("C:\Program Files\AutoCAD ART Prototype\Application.xls")
If Err Then
    Err.Clear
    Set excelapp = CreateObject("Excel.Application")
    excelapp.Visible = True
        If Err Then
        MsgBox Err.Description
        Exit Function
        End If
End If
MsgBox "Now running " & excelapp.Name
End Function

 

Function ExcelClose() ' This function closes the connection between excel and Autocad
Set excelapp = Nothing
End Function

 

Function GetCellValue(row As Integer, column As Integer) As Double
' This function returns the value of a given cell in excel.
GetCellValue = excelapp.activesheet.Cells(row, column).value
End Function

 

Sub ExcelToAcad()

Dim LTP_Origin_dist_262 As Double
Dim LTP_Origin_dist_136 As Double
Dim slope As Double

ExcelConnect

LTP_Origin_dist_262 = GetCellValue(14, 13)
LTP_Origin_dist_136 = GetCellValue(15, 13)
slope = GetCellValue(16, 13)

 

' Closing the excel-autocad link
ExcelClose
End Sub

0 Likes
3,788 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

Well, it has to be something wrong with ExcelConnect() procedure. After calling ExcelConnect(), you can add these lines of code to see if you get Excel Application and ActiveSheet object correctly:

 

ExcelConnect

 

If excelapp Is Nothing Then

  MsgBox "Something was wrong: cannot get Excel running!"

  Exit Sub

End If

 

If excelapp.ActiveSheet Is Nothing Then

  MsgBox "Something was wrong: no *.xls file open/no active worksheet!"

  Exit Sub

End If

 

''Then get value from the cell

z=GetCellValue(x,y)

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks a lot for your help.

 

I included the lines of code you suggested and apparently the problem comes form the "excelapp". The error message "Object required" is displayed and highlights the line "if excelapp Is Nothing Then".

 

I still don't understand why this is happening. Does this mean that the excel file is not found? I checked the path and it is correct. Do I need a command to open the excel file or create another object or do something else after GetObject?

 

Any help or idea would be greatly appreciated. Thanks!

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

My code is finally working! I deleted the functions and Ireplaced the name of the function by the actual command in the sub. The functions worked if I defined "excelapp" in each of them... as a consequence it is easier for me to gather everything in the sub.

0 Likes