Professional Documents
Culture Documents
by Kenny Ramage
Th s s a sample VBA rout ne wr tten for AutoCAD Rel 14. The appl cat on w ll demonstrate how to export data to M crosoft
Excel, perform calculat ons on that data and then re- mport the results of the calculat ons back nto AutoCAD.
Usage
Unz p the f les matl st.dvb, matl st.lsp, matl st.dwg and matl st.xls to your work ng d rectory.
Insert the matl st.Dwg. as a block nto any draw ng.
It should look l ke th s :
(If you have problems when wr t ng or runn ng th s projects, ensure that the M crosoft Excel 8.0 Object L brary s selected n
your VBA References.)
After runn ng the appl cat on you can nspect the results of the exported data n the f le matl st.xls. It should look someth ng
l ke th s :
The Source Code
Public acad As Object
Public doc As Object
Public ms As Object
Public ss As Object
Public ssnew As Object
Public Theatts As Variant
Public MsgBoxResp As Integer
'declare global variables
ssnew.Item(0).Update
'update the attribute block
End
End Sub
Sub UpdateAttrib(TagNumber As Integer, BTextString As String)
'This Sub Procedure tests the attribute data to check
'that is not a null value
Theatts(TagNumber).TextString = ""
'put a '-' place holder
Else
'if it is not empty
Theatts(TagNumber).TextString = BTextString
'use the attribute value
End If
End Sub
xlapp.Visible = True
'make Excel visible
xlapp.Windows("MATLIST.XLS").Visible = True
'make the workbook visible
'the 2 preceeding lines can be commented out if you
'do not want to see Excel in action
xlsheet.Cells(2, 1) = UserForm1.txt1.Text
xlsheet.Cells(3, 1) = UserForm1.txt8.Text
xlsheet.Cells(4, 1) = UserForm1.txt15.Text
xlsheet.Cells(5, 1) = UserForm1.txt22.Text
xlsheet.Cells(6, 1) = UserForm1.txt29.Text
xlsheet.Cells(2, 2) = UserForm1.txt2.Text
xlsheet.Cells(3, 2) = UserForm1.txt9.Text
xlsheet.Cells(4, 2) = UserForm1.txt16.Text
xlsheet.Cells(5, 2) = UserForm1.txt23.Text
xlsheet.Cells(6, 2) = UserForm1.txt30.Text
xlsheet.Cells(2, 3) = UserForm1.txt3.Text
xlsheet.Cells(3, 3) = UserForm1.txt10.Text
xlsheet.Cells(4, 3) = UserForm1.txt17.Text
xlsheet.Cells(5, 3) = UserForm1.txt24.Text
xlsheet.Cells(6, 3) = UserForm1.txt31.Text
xlsheet.Cells(2, 4) = UserForm1.txt4.Text
xlsheet.Cells(3, 4) = UserForm1.txt11.Text
xlsheet.Cells(4, 4) = UserForm1.txt18.Text
xlsheet.Cells(5, 4) = UserForm1.txt25.Text
xlsheet.Cells(6, 4) = UserForm1.txt32.Text
xlsheet.Cells(2, 5) = UserForm1.txt5.Text
xlsheet.Cells(3, 5) = UserForm1.txt12.Text
xlsheet.Cells(4, 5) = UserForm1.txt19.Text
xlsheet.Cells(5, 5) = UserForm1.txt26.Text
xlsheet.Cells(6, 5) = UserForm1.txt33.Text
xlsheet.Cells(2, 7) = UserForm1.txt7.Text
xlsheet.Cells(3, 7) = UserForm1.txt14.Text
xlsheet.Cells(4, 7) = UserForm1.txt21.Text
xlsheet.Cells(5, 7) = UserForm1.txt28.Text
xlsheet.Cells(6, 7) = UserForm1.txt35.Text
'fill the worksheet cells with the attribute values
UserForm1.txt6.Text = xlsheet.Cells(2, 6)
UserForm1.txt13.Text = xlsheet.Cells(3, 6)
UserForm1.txt20.Text = xlsheet.Cells(4, 6)
UserForm1.txt27.Text = xlsheet.Cells(5, 6)
UserForm1.txt34.Text = xlsheet.Cells(6, 6)
UserForm1.txt36.Text = xlsheet.Cells(7, 6)
'retrieve the calculated attribute values
xlbook.Close savechanges:=True
'save the changes in Excel
xlapp.Quit
'quit Excel
End Sub
Set ms = doc.ModelSpace
'set reference to model space
BlkG(0) = 2
'group code 2 for block name
TheBlock(0) = "MATLIST"
'the name of the attribute block
Theatts = ssnew.Item(0).GetAttributes
'get the attributes
UserForm1.txt1.Text = UCase(LTrim(Theatts(0).TextString))
'get the title attribute
'clear any leading spaces and
'convert to uppercase
UserForm1.txt2.Text = UCase(LTrim(Theatts(1).TextString))
UserForm1.txt3.Text = UCase(LTrim(Theatts(2).TextString))
UserForm1.txt4.Text = UCase(LTrim(Theatts(3).TextString))
UserForm1.txt5.Text = UCase(LTrim(Theatts(4).TextString))
UserForm1.txt6.Text = UCase(LTrim(Theatts(5).TextString))
UserForm1.txt7.Text = UCase(LTrim(Theatts(6).TextString))
UserForm1.txt8.Text = UCase(LTrim(Theatts(7).TextString))
UserForm1.txt9.Text = UCase(LTrim(Theatts(8).TextString))
UserForm1.txt10.Text = UCase(LTrim(Theatts(9).TextString))
UserForm1.txt11.Text = UCase(LTrim(Theatts(10).TextString))
UserForm1.txt12.Text = UCase(LTrim(Theatts(11).TextString))
UserForm1.txt13.Text = UCase(LTrim(Theatts(12).TextString))
UserForm1.txt14.Text = UCase(LTrim(Theatts(13).TextString))
UserForm1.txt15.Text = UCase(LTrim(Theatts(14).TextString))
UserForm1.txt16.Text = UCase(LTrim(Theatts(15).TextString))
UserForm1.txt17.Text = UCase(LTrim(Theatts(16).TextString))
UserForm1.txt18.Text = UCase(LTrim(Theatts(17).TextString))
UserForm1.txt19.Text = UCase(LTrim(Theatts(18).TextString))
UserForm1.txt20.Text = UCase(LTrim(Theatts(19).TextString))
UserForm1.txt21.Text = UCase(LTrim(Theatts(20).TextString))
UserForm1.txt22.Text = UCase(LTrim(Theatts(21).TextString))
UserForm1.txt23.Text = UCase(LTrim(Theatts(22).TextString))
UserForm1.txt24.Text = UCase(LTrim(Theatts(23).TextString))
UserForm1.txt25.Text = UCase(LTrim(Theatts(24).TextString))
UserForm1.txt26.Text = UCase(LTrim(Theatts(25).TextString))
UserForm1.txt27.Text = UCase(LTrim(Theatts(26).TextString))
UserForm1.txt28.Text = UCase(LTrim(Theatts(27).TextString))
UserForm1.txt29.Text = UCase(LTrim(Theatts(28).TextString))
UserForm1.txt30.Text = UCase(LTrim(Theatts(29).TextString))
UserForm1.txt31.Text = UCase(LTrim(Theatts(30).TextString))
UserForm1.txt32.Text = UCase(LTrim(Theatts(31).TextString))
UserForm1.txt33.Text = UCase(LTrim(Theatts(32).TextString))
UserForm1.txt34.Text = UCase(LTrim(Theatts(33).TextString))
UserForm1.txt35.Text = UCase(LTrim(Theatts(34).TextString))
UserForm1.txt36.Text = UCase(LTrim(Theatts(35).TextString))
UserForm1.txt1.SetFocus
UserForm1.txt1.SelStart = 0
UserForm1.txt1.SelLength = Len(UserForm1.txt1.Text)
'set the focus to the drawing title and highlight it
Else
'if no attribute title block is found
ThisDrawing.SelectionSets("TBLK").Delete
End
'end the application
End If
ThisDrawing.SelectionSets("TBLK").Delete
End Sub
UserForm1.Show
End Sub