Writing Ordinate Dimensions to Excel File

Writing Ordinate Dimensions to Excel File

Thomas.Long
Advocate Advocate
1,665 Views
7 Replies
Message 1 of 8

Writing Ordinate Dimensions to Excel File

Thomas.Long
Advocate
Advocate

So I'm fairly new to visual basic (less than a month of experience, please bear with me) so I apologize in advance if anything I ask for is stupid.

I have a secondary program that I want to port in the dimensions from an autocad file. Having them communicate directly is a bit over my head. However, I can make the other program observe the number of rows and port in the dimensions in excel. 

 

That being said, what I'm looking for is to:

 

1) Look in a specified location for a given file name, and delete it if its already there

2) Generate a new excel file with the same name

3) Select an unspecified number of ordinate dimensions and export them to the excel file

4) Change the dimensions from feet and inches (architectural) into just inches (decimal style)

 

The first two steps were just because I wanted to make absolutely sure that the file was always clean and when used would never accidentally have dimensions left over from previous uses.

 

The example of the desired output I just ignored the 0 dimension as we wont be selecting that.

 

Thank you all very much in advance and I apologize if anything I asked for was particularly stupid.

0 Likes
Accepted solutions (1)
1,666 Views
7 Replies
Replies (7)
Message 2 of 8

Thomas.Long
Advocate
Advocate

I've cobbled this together from some different codes I've found online, but I'm running into an issue. If anyone ever uses escape to exit the command then it doesn't close the excel file. This means the excel file is simultaneously open and not open, and I have no idea how to close it from inside the program.It's currently deleting the old file, creating a new one, and writing the dimensions to it. I just need to know how to fix this error now, because as it stands if it exits incorrectly it won't let you open the file because it says another user is using it (yourself) but it won't let you close the file because you don't actually have it open.

 

Option Explicit

Sub Main()

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in Excel
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)

Dim FileName As String
FileName = "H:\Beam Locations.xlsx"

If Not Dir(FileName) = "" Then Kill (FileName)

'Save the Workbook and Quit Excel
oBook.SaveAs FileName

Call SortDims(FileName, oExcel, oBook, oSheet)

oExcel.Save

oExcel.Quit

End Sub
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Function SortDims(strFileName, xlApp, xlBook, xlSheet)

Dim oSset As AcadSelectionSet
Dim oEnt As AcadEntity
Dim oDim As AcadDimension
Dim oDimOrd As AcadDimOrdinate
Dim eCnt As Integer
Dim iCnt As Integer
Dim rCnt As Integer
Dim iNdx As Integer
Dim insPnt() As Double
Dim fcode(0) As Integer
Dim fdata(0) As Variant
Dim dxfCode, dxfdata

fcode(0) = 0: fdata(0) = "DIMENSION"
dxfCode = fcode
dxfdata = fdata
Set oSset = ThisDrawing.PickfirstSelectionSet
oSset.Clear
oSset.SelectOnScreen dxfCode, dxfdata

iCnt = oSset.Count

ReDim SelPnt(0 To iCnt - 1, 0 To 3) As Variant
eCnt = 0

For Each oEnt In oSset

Set oDim = oEnt

insPnt = oDim.TextPosition
SelPnt(eCnt, 0) = insPnt(0)
SelPnt(eCnt, 1) = insPnt(1)
SelPnt(eCnt, 2) = insPnt(2)

Set oDimOrd = oEnt
SelPnt(eCnt, 3) = oDimOrd.Measurement

eCnt = eCnt + 1
Next oEnt

ReDim sortPnt(0 To iCnt - 1, 0 To 3) As Variant
sortPnt = ColSort(SelPnt, 1)

Dim irow As Long
irow = 1

With xlSheet
.Range("A:A").NumberFormat = "0.00#"
For iNdx = 0 To UBound(sortPnt, 1)
.Cells(irow, 1) = CStr(sortPnt(iNdx, 3))
irow = irow + 1
Next iNdx

End With

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Function ColSort(SourceArr As Variant, iPos As Integer) As Variant

Dim Check As Boolean
ReDim tmpArr(UBound(SourceArr, 2)) As Variant
Dim iCount As Integer
Dim jCount As Integer
Dim nCount As Integer

iPos = iPos - 1
Check = False

Do Until Check
Check = True
For iCount = LBound(SourceArr, 1) To UBound(SourceArr, 1) - 1
If SourceArr(iCount, iPos) > SourceArr(iCount + 1, iPos) Then
For jCount = LBound(SourceArr, 2) To UBound(SourceArr, 2)
tmpArr(jCount) = SourceArr(iCount, jCount)
SourceArr(iCount, jCount) = SourceArr(iCount + 1, jCount)
SourceArr(iCount + 1, jCount) = tmpArr(jCount)
Check = False
Next
End If
Next
Loop

ColSort = SourceArr

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

0 Likes
Message 3 of 8

Ed__Jobe
Mentor
Mentor

Working with Excel using COM, you need to explicitly dispose of any objects you create, such as oExcel, oBook and oSheet. You also, have to dispose of them in the opposite order in which you create them, i.e.

 

Set oSheet = Nothing

Set oBook = Nothing

Set oExcel = Nothing

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 8

Thomas.Long
Advocate
Advocate

Is that why its generating two extra files? I'm getting an extra file called resume.xlw in my C:\Users space and one called FileName there as well.

0 Likes
Message 5 of 8

Ed__Jobe
Mentor
Mentor

@Thomas.Long wrote:

Is that why its generating two extra files? I'm getting an extra file called resume.xlw in my C:\Users space and one called FileName there as well.


Possibly. Try deleting them and see if they reappear after you run your program again.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 8

Thomas.Long
Advocate
Advocate

They do. I've pretty much got the program running with one slight error. If I exit the function early it doesn't exit excel. I'm trying to finish it off with a keystroke check command that says if you hit the escape key to close excel, but I'm not quite sure hot to place it. Everything works perfectly except the two lines commented out directly under Sub Export(). Those are the two lines I hope to use to make some form of command to exit on the statement. 

Right now, if I run the code all the way through it works flawlessly. However, if I hit escape to exit the command it never closes the excel function and so to access the file I have to use task manager to end the process manually.

 

Option Explicit

Sub Export()

'Dim KeyAscii As MSForms.ReturnInteger
'If Asc(Chr(KeyAscii)) = 27 Then

If Not Dir("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW") = "" Then Kill ("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW")

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in Excel
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)

If Not Dir("H:\Beam Locations.xlsx") = "" Then Kill ("H:\Beam Locations.xlsx")

'Save the Workbook and Quit Excel
oBook.SaveAs "H:\Beam Locations.xlsx"

Call SortDims("H:\Beam Locations.xlsx", oExcel, oBook, oSheet)

oExcel.Save
oExcel.Quit

Set oSheet = Nothing
Set oBook = Nothing
Set oExcel = Nothing

End Sub
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Function SortDims(strFileName, xlApp, xlBook, xlSheet)

Dim oSset As AcadSelectionSet
Dim oEnt As AcadEntity
Dim oDim As AcadDimension
Dim oDimOrd As AcadDimOrdinate
Dim eCnt As Integer
Dim iCnt As Integer
Dim rCnt As Integer
Dim iNdx As Integer
Dim insPnt() As Double
Dim fcode(0) As Integer
Dim fdata(0) As Variant
Dim dxfCode, dxfdata

fcode(0) = 0: fdata(0) = "DIMENSION"
dxfCode = fcode
dxfdata = fdata
Set oSset = ThisDrawing.PickfirstSelectionSet
oSset.Clear
oSset.SelectOnScreen dxfCode, dxfdata

iCnt = oSset.Count

ReDim SelPnt(0 To iCnt - 1, 0 To 3) As Variant
eCnt = 0

For Each oEnt In oSset

Set oDim = oEnt

insPnt = oDim.TextPosition
SelPnt(eCnt, 0) = insPnt(0)
SelPnt(eCnt, 1) = insPnt(1)
SelPnt(eCnt, 2) = insPnt(2)

Set oDimOrd = oEnt
SelPnt(eCnt, 3) = oDimOrd.Measurement

eCnt = eCnt + 1
Next oEnt

ReDim sortPnt(0 To iCnt - 1, 0 To 3) As Variant
sortPnt = ColSort(SelPnt, 1)

Dim irow As Long
irow = 1

With xlSheet
.Range("A:A").NumberFormat = "0.00#"
For iNdx = 0 To UBound(sortPnt, 1)
.Cells(irow, 1) = CStr(sortPnt(iNdx, 3))
irow = irow + 1
Next iNdx

End With

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Function ColSort(SourceArr As Variant, iPos As Integer) As Variant

Dim Check As Boolean
ReDim tmpArr(UBound(SourceArr, 2)) As Variant
Dim iCount As Integer
Dim jCount As Integer
Dim nCount As Integer

iPos = iPos - 1
Check = False

Do Until Check
Check = True
For iCount = LBound(SourceArr, 1) To UBound(SourceArr, 1) - 1
If SourceArr(iCount, iPos) > SourceArr(iCount + 1, iPos) Then
For jCount = LBound(SourceArr, 2) To UBound(SourceArr, 2)
tmpArr(jCount) = SourceArr(iCount, jCount)
SourceArr(iCount, jCount) = SourceArr(iCount + 1, jCount)
SourceArr(iCount + 1, jCount) = tmpArr(jCount)
Check = False
Next
End If
Next
Loop

ColSort = SourceArr

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

0 Likes
Message 7 of 8

Ed__Jobe
Mentor
Mentor
Accepted solution

This post will help with the ESC key, but not all cases that might cancel a command.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 8 of 8

Thomas.Long
Advocate
Advocate

Thank you! That worked perfectly! Posting in case anyone else needs to port ordinate dimensions to an excel file.

 

Option Explicit

Sub Export()

    On Error GoTo ErrorHandler

    If Not Dir("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW") = "" Then Kill ("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW")
    
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Add
    Set oSheet = oBook.Worksheets(1)
    
    If Not Dir("H:\Beam Locations.xlsx") = "" Then Kill ("H:\Beam Locations.xlsx")
    
    'Save the Workbook and Quit Excel
    oBook.SaveAs "H:\Beam Locations.xlsx"
    
    Call SortDims("H:\Beam Locations.xlsx", oExcel, oBook, oSheet)
    
    oExcel.Save
    oExcel.Quit
    
    Set oSheet = Nothing
    Set oBook = Nothing
    Set oExcel = Nothing
    
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ErrorHandler:
    If Err.Number = -2147352567 Then
    
        oExcel.Save
        oExcel.Quit
    
        Set oSheet = Nothing
        Set oBook = Nothing
        Set oExcel = Nothing
    
        Exit Sub
    End If
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
End Sub
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Function SortDims(strFileName, xlApp, xlBook, xlSheet)

    On Error GoTo ErrorHandler

    Dim oSset As AcadSelectionSet
    Dim oEnt As AcadEntity
    Dim oDim As AcadDimension
    Dim oDimOrd As AcadDimOrdinate
    Dim eCnt As Integer
    Dim iCnt As Integer
    Dim rCnt As Integer
    Dim iNdx As Integer
    Dim insPnt() As Double
    Dim fcode(0) As Integer
    Dim fdata(0) As Variant
    Dim dxfCode, dxfdata

    fcode(0) = 0: fdata(0) = "DIMENSION"
    dxfCode = fcode
    dxfdata = fdata
    Set oSset = ThisDrawing.PickfirstSelectionSet
    oSset.Clear
    oSset.SelectOnScreen dxfCode, dxfdata

    iCnt = oSset.Count

    ReDim SelPnt(0 To iCnt - 1, 0 To 3) As Variant
    eCnt = 0

    For Each oEnt In oSset

        Set oDim = oEnt

        insPnt = oDim.TextPosition
        SelPnt(eCnt, 0) = insPnt(0)
        SelPnt(eCnt, 1) = insPnt(1)
        SelPnt(eCnt, 2) = insPnt(2)

        Set oDimOrd = oEnt
        SelPnt(eCnt, 3) = oDimOrd.Measurement
            
        eCnt = eCnt + 1
    Next oEnt

    ReDim sortPnt(0 To iCnt - 1, 0 To 3) As Variant
    sortPnt = ColSort(SelPnt, 1)

    Dim irow As Long
    irow = 1
    
    With xlSheet
        .Range("A:A").NumberFormat = "0.00#"
        For iNdx = 0 To UBound(sortPnt, 1)
            .Cells(irow, 1) = CStr(sortPnt(iNdx, 3))
            irow = irow + 1
        Next iNdx

    End With
    
    '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ErrorHandler:
    If Err.Number = -2147352567 Then
    
        xlApp.Save
        xlApp.Quit
    
        Set xlSheet = Nothing
        Set xlBook = Nothing
        Set xlApp = Nothing
    
        Exit Function
    End If
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Function ColSort(SourceArr As Variant, iPos As Integer) As Variant

    Dim Check As Boolean
    ReDim tmpArr(UBound(SourceArr, 2)) As Variant
    Dim iCount As Integer
    Dim jCount As Integer
    Dim nCount As Integer

    iPos = iPos - 1
    Check = False

    Do Until Check
        Check = True
        For iCount = LBound(SourceArr, 1) To UBound(SourceArr, 1) - 1
            If SourceArr(iCount, iPos) > SourceArr(iCount + 1, iPos) Then
                For jCount = LBound(SourceArr, 2) To UBound(SourceArr, 2)
                    tmpArr(jCount) = SourceArr(iCount, jCount)
                    SourceArr(iCount, jCount) = SourceArr(iCount + 1, jCount)
                    SourceArr(iCount + 1, jCount) = tmpArr(jCount)
                    Check = False
                Next
            End If
        Next
    Loop

    ColSort = SourceArr

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



0 Likes