Hi everyone,
I got help from cadtutor.net where someone posted the code for a short program that is useful to me. One part, however does not work but it apparently works on their computers. I think it has something to do with References.
Public Sub ExportDims()
Dim oEnt As AcadEntity
Dim oDim As AcadDimRotated
Dim oOle As AcadOle
Dim mea1 As Double
Dim mea2 As Double
Dim pickPt As Variant
When I run it, I get a User-defined type not defined and the AcadOle line is selected and the Public Sub ExportDims() is highlighted in yellow.
I have added the following references with no success:
Microsoft DAO 3.6 Object Library
Microsoft OLE DB Error Libarary
oleprn 1.0 Type Library
Microsoft ActiveX Data Objects 2.7 Library
OLE Automation
This is the full code if anyone needs the big picture.
'' require reference to Microsoft Excel XX.X Object Library
Option Explicit
Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Display and use the File open dialog
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ShowOpen() As String
Dim strTemp As String
Dim VertName As OPENFILENAME
VertName.lStructSize = Len(VertName)
VertName.hwndOwner = ThisDrawing.HWND
VertName.lpstrFilter = "All Excel Files (*.xls)" + Chr$(0) + _
"*.xls" + Chr$(0) + " | " + "Excel Files (*.xlsx)" + Chr$(0) + _
"*.xlsx"
VertName.lpstrFile = Space$(254)
VertName.nMaxFile = 255
VertName.lpstrFileTitle = Space$(254)
VertName.nMaxFileTitle = 255
VertName.lpstrInitialDir = CurDir
VertName.lpstrTitle = "Select Excel File"
VertName.flags = 0
If GetOpenFileName(VertName) Then
strTemp = (Trim(VertName.lpstrFile))
ShowOpen = Mid(strTemp, 1, Len(strTemp) - 1)
End If
End Function
'' Modified 10/8/03 to remove early binding and
'' include late binding. Now should work with
'' any version Excel
Function IsExcelRunning() As Boolean
Dim objXL As Object
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
IsExcelRunning = (Err.Number = 0)
Set objXL = Nothing
Err.Clear
End Function
'' based on macros written by Jeff Mishler
'Changed the way Excel is loaded per suggestion by
'Randall Rath - http://www.vbdesign.net/
'which also added the "Function IsExcelRunning"
Public Sub ExportDims()
Dim oEnt As AcadEntity
Dim oDim As AcadDimRotated
Dim oOle As AcadOle
Dim mea1 As Double
Dim mea2 As Double
Dim pickPt As Variant
ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select length dimension >> "
If Not TypeOf oEnt Is AcadDimension Then Exit Sub
Set oDim = oEnt
mea1 = oDim.Measurement
ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select width dimension >> "
If Not TypeOf oEnt Is AcadDimension Then Exit Sub
Set oDim = oEnt
mea2 = oDim.Measurement
ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select embedded table >> "
If Not TypeOf oEnt Is AcadOle Then Exit Sub
Set oOle = oEnt
Dim xlFileName As String
'***Begin code from Randall Rath******
Dim oXL As Object
Dim blnXLRunning As Boolean
blnXLRunning = IsExcelRunning()
If blnXLRunning Then
Set oXL = GetObject(, "Excel.Application")
Else
Set oXL = CreateObject("Excel.Application")
oXL.Visible = False
oXL.UserControl = False
oXL.DisplayAlerts = False
End If
'***End code from Randall Rath******
Dim oWb As Object
Dim oWs As Object
xlFileName = ShowOpen()
Set oWb = oXL.Workbooks.Open(xlFileName)
If oWb Is Nothing Then
MsgBox "The Excel file " & xlFileName & " not found" & _
"Try again."
GoTo Exit_Here
End If
Set oWs = oWb.Worksheets("Sheet1")
oWs.Activate
' write data to Excel
oWs.Columns(1).NumberFormat = "@"
oWs.Columns(2).NumberFormat = "0.00"
oWs.Columns(3).NumberFormat = "0.00"
oWs.Cells(2, 1) = "-001"
oWs.Cells(2, 2) = mea1
oWs.Cells(2, 3) = mea2
oWs.Columns.AutoFit
Exit_Here:
Set oWs = Nothing
oWb.Save: oWb.Close
Set oWb = Nothing
oXL.Quit
Set oXL = Nothing
DoEvents
MsgBox "Done"
End Sub
Bookmarks