Message 1 of 8
How to open an existing OLE excel worksheet inside the AutoCAD?

Not applicable
11-20-2007
10:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
Previously I have written a small macro so it will read an excel sheet as an indepedent file and compare the excel table contents with text objects found inside the autocad drawing (for double checking purpose). Now when we recently upgrade to AutoCAD 2008, the excel table now become embeded as an OLE object inside the drawing and thus not an independent file. Now I don't know how to make the code to read this excel sheet inside the drawing. It should be something simple but I coudn't find the exact code. Any helps will be welcomed as I am new to VBA. Here is my original subroutine:
Sub readexcel(filename, part_name_2, part_total_2)
Dim Excel As Object
Dim i As Integer
Dim excelSheet As Object
Dim k As Boolean, x As Integer, y As Integer
' Open Excel
On Error Resume Next
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
If Dir(filename) = "" Then
MsgBox ("Error: Can not find Excel " & filename & " file.")
End
End If
Excel.Visible = True
Excel.Workbooks.Open (filename)
Excel.Sheets(1).Select
Set excelSheet = Excel.ActiveWorkbook.Sheets(1)
x = 1
part_total_2 = 0
Do While x < 16
k = False
For y = 1 To 37
excelSheet.Cells(y, x).Select
If Trim(excelSheet.Cells(y, x)) = "PARTS NO." Then k = True
If k = True Then
If Excel.ActiveCell.Interior.ColorIndex = -4142 And _
LTrim(excelSheet.Cells(y, x)) <> "" And _
Left(excelSheet.Cells(y, x), 1) <> "(" And _
Left(excelSheet.Cells(y, x), 2) <> "GK" And _
Left(excelSheet.Cells(y, x), 3) <> "SPG" And _
Left(excelSheet.Cells(y, x), 2) <> "B_" Then
part_total_2 = part_total_2 + 1
part_name_2(part_total_2) = CStr(excelSheet.Cells(y, x))
End If
End If
Next y
If k = True Then x = x + 3 Else x = x + 1
k = False
Loop
Excel.ActiveWorkbook.Close SaveChanges:=False
Excel.Quit
End Sub
Thanks,
Joseph
Previously I have written a small macro so it will read an excel sheet as an indepedent file and compare the excel table contents with text objects found inside the autocad drawing (for double checking purpose). Now when we recently upgrade to AutoCAD 2008, the excel table now become embeded as an OLE object inside the drawing and thus not an independent file. Now I don't know how to make the code to read this excel sheet inside the drawing. It should be something simple but I coudn't find the exact code. Any helps will be welcomed as I am new to VBA. Here is my original subroutine:
Sub readexcel(filename, part_name_2, part_total_2)
Dim Excel As Object
Dim i As Integer
Dim excelSheet As Object
Dim k As Boolean, x As Integer, y As Integer
' Open Excel
On Error Resume Next
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
If Dir(filename) = "" Then
MsgBox ("Error: Can not find Excel " & filename & " file.")
End
End If
Excel.Visible = True
Excel.Workbooks.Open (filename)
Excel.Sheets(1).Select
Set excelSheet = Excel.ActiveWorkbook.Sheets(1)
x = 1
part_total_2 = 0
Do While x < 16
k = False
For y = 1 To 37
excelSheet.Cells(y, x).Select
If Trim(excelSheet.Cells(y, x)) = "PARTS NO." Then k = True
If k = True Then
If Excel.ActiveCell.Interior.ColorIndex = -4142 And _
LTrim(excelSheet.Cells(y, x)) <> "" And _
Left(excelSheet.Cells(y, x), 1) <> "(" And _
Left(excelSheet.Cells(y, x), 2) <> "GK" And _
Left(excelSheet.Cells(y, x), 3) <> "SPG" And _
Left(excelSheet.Cells(y, x), 2) <> "B_" Then
part_total_2 = part_total_2 + 1
part_name_2(part_total_2) = CStr(excelSheet.Cells(y, x))
End If
End If
Next y
If k = True Then x = x + 3 Else x = x + 1
k = False
Loop
Excel.ActiveWorkbook.Close SaveChanges:=False
Excel.Quit
End Sub
Thanks,
Joseph