+ All Categories
Home > Documents > AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration...

AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration...

Date post: 11-Mar-2018
Category:
Upload: lythien
View: 233 times
Download: 9 times
Share this document with a friend
16
AutoCAD and VBA Integration with Microsoft Office Autodesk University 2003— Jerry Winters Pg. 1 CP42-1 AutoCAD and VBA Integration with Microsoft Office CP42-1 Presented by Jerry Winters About this Class Classes are taught on AutoCAD / Excel Integration. Classes are taught on AutoCAD / Access Integration. This class will utilize most Microsoft Office applications. Specifically, we will use Microsoft Excel, Microsoft Word, Microsoft Out- look, Microsoft Access, and Microsoft FrontPage. These tools will be used to create a solution to allow us to standardize an AutoCAD drawing, request quotes from suppliers, process the quotes, and show resulting data for use on a corporate Intranet. About the Instructor Jerry Winters has been using AutoCAD since 1988. He has written several books on programming AutoCAD with VBA and will be releasing his AutoCAD 2004 VBA book soon. Jerry provides VBA training as well as customization services for customers all over the United States as well as other countries on Earth. He has yet to teach on other planets in the solar system but is not counting out the possibility in the future. This is his second year teaching at Autodesk University. Had his 5th son, Nathan not been born the night before AU 2001, this would have been his third year. His first daughter, Olivia was born October 28th, 2003, so he is very happy to be here this year. He has been happily married for 12 years. If you are counting, that makes 5 sons, 1 daughter, and 1 wife. Also, 8 chickens, 1 cat, 1 turkey named Thanksgiving (he isn’ t feeling very well) and 1 turkey named Christmas (she is very cold right now). Jerry and his 3 oldest sons hold Tae Kwon Do Black Belts. To contact Jerry, please email him at [email protected] or visit his website: www.vbcad.com
Transcript
Page 1: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 1 CP42-1

AutoCAD and VBA Integration with Microsoft Office CP42-1

Presented by Jerry Winters About this Class Classes are taught on AutoCAD / Excel Integration. Classes are taught on AutoCAD / Access Integration. This class will utilize most Microsoft Office applications. Specifically, we will use Microsoft Excel, Microsoft Word, Microsoft Out-look, Microsoft Access, and Microsoft FrontPage. These tools will be used to create a solution to allow us to standardize an AutoCAD drawing, request quotes from suppliers, process the quotes, and show resulting data for use on a corporate Intranet. About the Instructor Jerry Winters has been using AutoCAD since 1988. He has written several books on programming AutoCAD with VBA and will be releasing his AutoCAD 2004 VBA book soon. Jerry provides VBA training as well as customization services for customers all over the United States as well as other countries on Earth. He has yet to teach on other planets in the solar system but is not counting out the possibility in the future. This is his second year teaching at Autodesk University. Had his 5th son, Nathan not been born the night before AU 2001, this would have been his third year. His first daughter, Olivia was born October 28th, 2003, so he is very happy to be here this year. He has been happily married for 12 years. If you are counting, that makes 5 sons, 1 daughter, and 1 wife. Also, 8 chickens, 1 cat, 1 turkey named Thanksgiving (he isn’t feeling very well) and 1 turkey named Christmas (she is very cold right now). Jerry and his 3 oldest sons hold Tae Kwon Do Black Belts. To contact Jerry, please email him at [email protected] or visit his website: www.vbcad.com

Page 2: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 2 CP42-1

The file “C:\Program Files\AutoCAD 2004\Sample\8th floor furniture.dwg” is installed with AutoCAD 2004. We will use this file for this class. We will pretend that we have received this file from a 3rd party. The first thing we will do is a little standardization.

Page 3: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 3 CP42-1

The first thing we notice is the block names of some of the inserted blocks. Let’s get rid of much of the name and simplify the name. This block that is listed should be named “DESK_CHAIR”.

Sub ChangeBlockName() Dim MyBlk As AcadBlock For Each MyBlk In ThisDrawing.Blocks If Left(MyBlk.Name, 1) <> "*" Then If InStr(1, MyBlk.Name, "http") > 0 Then MyBlk.Name = Mid(MyBlk.Name, InStrRev(MyBlk.Name, ".") + 1) End If End If Next End Sub This code looks for the string “http” in the name of the block. If found, we take everything to the right of the left-most period in the block name. The next thing we notice is that the blocks we want to work with have a single attribute. Our standard dictates that we have three attributes named “dbID”, “cost”, and “supplier”. How do you add an attribute to a block that is already inserted? One way is to add an attribute to the block definition and swap out the old inserted blocks with the newly defined blocks. Sub AddAtts() Dim BlkDef As AcadBlock Dim InsPt(0 To 2) As Double For Each BlkDef In ThisDrawing.Blocks If Left(BlkDef.Name, 1) <> "*" And BlkDef.IsXRef = False Then If MsgBox("Add attributes to " & BlkDef.Name & "?", vbYesNo) = vbYes Then BlkDef.AddAttribute 1, acAttributeModeInvisible, "dbID", InsPt, "dbID", "" BlkDef.AddAttribute 1, acAttributeModeInvisible, "cost", InsPt, "cost", "" BlkDef.AddAttribute 1, acAttributeModeInvisible, "supplier", InsPt, "supplier", "" End If End If Next End Sub

Page 4: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 4 CP42-1

The macro AddAtts asks if we want to add our standard attributes to each block. Clicking “Yes” means the attributes will be added to the block. Clicking “No” means the block will not be modified. Sub AskToSwap() Dim MySS As AcadSelectionSet Dim BlkRef As AcadBlockReference Dim GrpC(0 To 1) As Integer Dim GrpV(0 To 1) As Variant Dim AcadBlk As AcadBlock GrpC(0) = 0: GrpV(0) = "INSERT" For Each AcadBlk In ThisDrawing.Blocks If Left(AcadBlk.Name, 1) <> "*" And AcadBlk.IsXRef = False Then If MsgBox("Swap block " & AcadBlk.Name & "?", vbYesNo) = vbYes Then Set MySS = ThisDrawing.SelectionSets.Add("BlkSwap") GrpC(1) = 2: GrpV(1) = AcadBlk.Name MySS.Select acSelectionSetAll, , , GrpC, GrpV For Each BlkRef In MySS SwapBlocks BlkRef, AcadBlk.Name Next MySS.Delete End If End If Next End Sub Sub SwapBlocks(MyBlk As AcadBlockReference, BlockToReplaceWith As String) Dim TmpBlk As AcadBlockReference Set TmpBlk = ThisDrawing.ModelSpace.InsertBlock(MyBlk.InsertionPoint, _ BlockToReplaceWith, MyBlk.XScaleFactor, MyBlk.YScaleFactor, _ MyBlk.ZScaleFactor, MyBlk.Rotation) TmpBlk.Layer = MyBlk.Layer MyBlk.Delete End Sub The macro “AskToSwap” goes through each block in the drawing and asks if you want to swap the existing insertions of the block with the existing definitions of the blocks. If the user says “Yes” to the message box, we create a selection set of all blocks of that name and call the SwapBlocks procedure to do the swap.

Page 5: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 5 CP42-1

Sub GetRefs() Dim MyBlk As AcadBlock Dim MyExcel As Excel.Application Dim MySht As Excel.worksheet Dim CurRow As Long Dim MySS As AcadSelectionSet Dim GrpC(0 To 1) As Integer Dim GrpV(0 To 1) As Variant CurRow = 1 Set MyExcel = GetObject(, "Excel.Application") Set MySht = MyExcel.ActiveSheet Dim CurCol As Long Dim BlkRef As AcadBlockReference For Each MyBlk In ThisDrawing.Blocks If Left(MyBlk.Name, 1) <> "*" And MyBlk.IsXRef = False Then If MsgBox("Export Block " & MyBlk.Name & "?", vbYesNo) = vbYes Then CurCol = 3 GrpC(0) = 0: GrpV(0) = "INSERT" GrpC(1) = 2: GrpV(1) = MyBlk.Name Set MySS = ThisDrawing.SelectionSets.Add("GetRefs") MySS.Select acSelectionSetAll, , , GrpC, GrpV For Each BlkRef In MySS MySht.Cells(CurRow, 1) = MyBlk.Name MySht.Cells(CurRow, 2).NumberFormat = "@" MySht.Cells(CurRow, 2) = BlkRef.Handle Dim cadent As AcadEntity CurCol = 3 For Each cadent In MyBlk If cadent.ObjectName = "AcDbAttributeDefinition" Then MySht.Cells(CurRow, CurCol).AddComment MySht.Cells(CurRow, CurCol).Comment.Visible = False MySht.Cells(CurRow, CurCol).Comment.Text cadent.TagString CurCol = CurCol + 1 End If Next CurRow = CurRow + 1 Next MySS.Delete End If End If Next End Sub Now that the blocks have been updated, we need a way to update the attributes of the blocks. We will do this by bringing all of the blocks and their attributes into Excel. The Attribute tag name is entered in Excel as a comment so we know which cell relates to which attribute.

Page 6: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 6 CP42-1

The last attribute column is for the supplier. Excel makes it easy to copy and paste suppliers down the list of inserted blocks. Let’s sort the columns now based on the supplier first, and then by the block name.

Page 7: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 7 CP42-1

Sub UpdateAttsFromExcel() Dim MyBlk As AcadBlockReference Dim MyExcel As Excel.Application Dim MySht As Excel.worksheet Dim CurRow As Long CurRow = 1 CurCol = 3 Set MyExcel = GetObject(, "Excel.Application") Set MySht = MyExcel.ActiveSheet While MySht.Cells(CurRow, 2) <> "" Dim MyComm As Comment Dim IsGood As Boolean Set MyComm = MySht.Cells(CurRow, CurCol).Comment While MyComm Is Nothing = False Set MyBlk = ThisDrawing.HandleToObject(MySht.Cells(CurRow, 2)) atts = MyBlk.GetAttributes For I = LBound(atts) To UBound(atts) If atts(I).TagString = MySht.Cells(CurRow, CurCol).Comment.Text Then If MySht.Cells(CurRow, CurCol) <> "" Then atts(I).TextString = MySht.Cells(CurRow, CurCol) End If I = UBound(atts) + 1 End If Next I CurCol = CurCol + 1 Set MyComm = MySht.Cells(CurRow, CurCol).Comment Wend CurRow = CurRow + 1 CurCol = 3 Wend End Sub This macro will take the data we have in Excel and update the blocks in AutoCAD to reflect the values in Excel.

After the macro is run, the ‘Supplier’ attribute is filled with whatever was in Excel.

Page 8: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 8 CP42-1

It makes sense to have bi-directional updating of data so we will create a macro that updates values in Excel based on changes to attributes in AutoCAD. Sub UpdateAttsFromAutoCAD() Dim MyBlk As AcadBlockReference Dim MyExcel As Excel.Application Dim MySht As Excel.worksheet Dim CurRow As Long CurRow = 1 CurCol = 3 Set MyExcel = GetObject(, "Excel.Application") Set MySht = MyExcel.ActiveSheet While MySht.Cells(CurRow, 2) <> "" Dim MyComm As Comment Dim IsGood As Boolean Set MyComm = MySht.Cells(CurRow, CurCol).Comment While MyComm Is Nothing = False Set MyBlk = ThisDrawing.HandleToObject(MySht.Cells(CurRow, 2)) atts = MyBlk.GetAttributes For I = LBound(atts) To UBound(atts) If atts(I).TagString = MySht.Cells(CurRow, CurCol).Comment.Text Then MySht.Cells(CurRow, CurCol) = atts(I).TextString I = UBound(atts) + 1 End If Next I CurCol = CurCol + 1 Set MyComm = MySht.Cells(CurRow, CurCol).Comment Wend CurRow = CurRow + 1 CurCol = 3 Wend End Sub So far we have worked with Microsoft Excel. We now need to take the data we have exported into Excel and request quotes from the suppliers specified in the blocks.

Page 9: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 9 CP42-1

Sub PrepareQuotes() Dim MyExcel As Excel.Application Dim MySht As Excel.worksheet Dim CurRow As Long CurRow = 1 CurCol = 3 Set MyExcel = GetObject(, "Excel.Application") Set MySht = MyExcel.ActiveSheet Dim BlkCnt As Long Dim BlkName As String Dim SupName As String SupName = MySht.Cells(1, 6) BlkName = MySht.Cells(1, 1) PrepareNextDoc While MySht.Cells(CurRow, 2) <> "" If MySht.Cells(CurRow, 6) <> SupName Then AddBlockToQuote BlkName, BlkCnt SaveDoc SupName PrepareNextDoc BlkName = MySht.Cells(CurRow, 1) SupName = MySht.Cells(CurRow, 6) BlkCnt = 1 Else If MySht.Cells(CurRow, 1) <> BlkName Then AddBlockToQuote BlkName, BlkCnt BlkName = MySht.Cells(CurRow, 1) BlkCnt = 1 Else BlkCnt = BlkCnt + 1 End If End If CurRow = CurRow + 1 Wend AddBlockToQuote BlkName, BlkCnt SaveDoc SupName End Sub I have put the procedures we need to create in BOLD. They will follow this page.

Page 10: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 10 CP42-1

Sub SaveDoc(Supplier As String) Dim MyWord As Word.Application Dim MyWDoc As Word.Document Set MyWord = GetObject(, "Word.Application") Set MyWDoc = MyWord.ActiveDocument MyWDoc.SaveAs "c:\au2003\office\out\" & Supplier & ".doc" MyWDoc.Close End Sub Sub PrepareNextDoc() Dim MyWord As Word.Application Dim MyWDoc As Word.Document Set MyWord = GetObject(, "Word.Application") Set MyWDoc = MyWord.Documents.Add("RequestForQuote.dot") End Sub Sub AddBlockToQuote(BName As String, BCnt As Long) Dim MyWord As Word.Application Dim MyWDoc As Word.Document Set MyWord = GetObject(, "Word.Application") Set MyWDoc = MyWord.ActiveDocument Dim MyTable As Table Set MyTable = MyWDoc.Tables(1) 'Insert Row and Populate MyTable.Cell(MyTable.Rows.Count, 1).Select MyWord.Selection.InsertRowsBelow 1 MyTable.Cell(MyTable.Rows.Count, 1).Range.Text = MyTable.Rows.Count - 1 MyTable.Cell(MyTable.Rows.Count, 2).Range.Font.Bold = False MyTable.Cell(MyTable.Rows.Count, 3).Range.Font.Bold = False MyTable.Cell(MyTable.Rows.Count, 4).Range.Font.Bold = False MyTable.Cell(MyTable.Rows.Count, 5).Range.Font.Bold = False MyTable.Cell(MyTable.Rows.Count, 4).Range.ParagraphFormat.Alignment = 0 'Left MyTable.Cell(MyTable.Rows.Count, 2).Range.Text = BCnt MyTable.Cell(MyTable.Rows.Count, 4).Range.Text = BName End Sub The code we have so far will create Microsoft Word documents based on a template named “RequestForQuote.dot”. We update the table identifying the items we want the supplier to quote. The files are saved to “c:\au2003\office\out’. When our suppliers return the documents, we will put them in the ‘in’ folder. Now that we have the Word documents created, we need to email them. Let’s use Microsoft Outlook to email them out and add tasks for each email to make sure we follow up on the request for quotes.

Page 11: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 11 CP42-1

Sub SendRFQ() Dim MyFSO As New FileSystemObject Dim MyFolder As Folder Dim MyFile As File Set MyFolder = MyFSO.GetFolder("c:\au2003\office\in") For Each MyFile In MyFolder.Files Select Case MyFile.Type Case "Microsoft Word Document" Select Case UCase(MyFile.Name) Case "HP.DOC" EmailDoc "[email protected]", MyFile.Path, "HP" Case "HERMANMILLER.DOC" EmailDoc "[email protected]", MyFile.Path, "Herman Miller" Case "AT&T.DOC" EmailDoc "[email protected]", MyFile.Path, "AT&T" End Select End Select Next End Sub Sub EmailDoc(MailAddress As String, FilePath As String, Company As String) Dim MyOut As New Outlook.Application Dim MyMail As MailItem Set MyMail = MyOut.CreateItem(olMailItem) MyMail.To = MailAddress MyMail.Subject = "Please provide a quote for the items included in this RFQ." MyMail.Attachments.Add FilePath MyMail.Body = "Thank you." & vbCr & vbCr & "Jerry Winters" MyMail.Send Dim MyTask As TaskItem Set MyTask = MyOut.CreateItem(olTaskItem) MyTask.Body = "Look for returned request from " & Company MyTask.DueDate = DateAdd("d", 4, Now) MyTask.Subject = "RFQ sent to " & Company MyTask.Save End Sub I have bolded the lines where the email is sent and the task is saved to Outlook. At this point, the supplier has our RFQ. The supplier needs to enter the costs in the provided space in the table in the

RFQ. The supplier simply needs to enter the value in the ‘Price’ column, save it, and email it back to us. When it comes back, we place the file in the ‘c:\au2003\office\in’ directory.

Page 12: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 12 CP42-1

Sub GetPricingFromWord() Dim MyFSO As New FileSystemObject Dim MyFolder As Folder Dim MyFile As File Dim MyWord As Word.Application Dim MyWDoc As Word.Document Set MyWord = GetObject(, "Word.Application") Set MyFolder = MyFSO.GetFolder("c:\au2003\office\in") For Each MyFile In MyFolder.Files Select Case MyFile.Type Case "Microsoft Word Document" Set MyWDoc = MyWord.Documents.Open(MyFile.Path) Dim MyTable As Table Set MyTable = MyWDoc.Tables(1) For I = 2 To MyTable.Rows.Count Dim ItmCost As Double Dim BlockName As String ItmCost = Left(MyTable.Cell(I, 5).Range.Text, _ InStr(1, MyTable.Cell(I, 5).Range.Text, vbCr) - 1) BlockName = Left(MyTable.Cell(I, 4).Range.Text, _ InStr(1, MyTable.Cell(I, 4).Range.Text, vbCr) - 1) Select Case UCase(MyFile.Name) Case "HP.DOC" UpdateCost BlockName, ItmCost, "HP" Case "HERMANMILLER.DOC" UpdateCost BlockName, ItmCost, "Herman Miller" Case "AT&T.DOC" UpdateCost BlockName, ItmCost, "AT&T" End Select Next I MyWDoc.Close False End Select Next Exit Sub End Sub Sub UpdateCost(BName As String, BCost As Double, Supplier As String) Dim MySS As AcadSelectionSet Dim GrpC(0 To 1) As Integer Dim GrpV(0 To 1) As Variant Dim MyBlk As AcadBlockReference GrpC(0) = 0: GrpV(0) = "INSERT" GrpC(1) = 2: GrpV(1) = BName Set MySS = ThisDrawing.SelectionSets.Add("UpdateCost") MySS.Select acSelectionSetAll, , , GrpC, GrpV For Each MyBlk In MySS atts = MyBlk.GetAttributes atts(2).TextString = BCost SendToDB MyBlk.Name, MyBlk.Handle, BCost, Supplier Next MySS.Delete End Sub

Page 13: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 13 CP42-1

Sub SendToDB(BName As String, BHandle As String, BCost As Double, Supplier As String) Dim MyDB As New ADODB.Connection Dim MyRS As New ADODB.Recordset MyDB.Open "File name=c:\udls\AU2003office.udl" MyDB.Execute "Delete from Blocks Where BHandle = '" & BHandle & "'" MyRS.Open "Blocks", MyDB, 1, 2 MyRS.AddNew MyRS("BHandle") = BHandle MyRS("BName") = BName MyRS("BCost") = BCost MyRS("BSupplier") = Supplier MyRS.Update MyRS.Close MyDB.Close Set MyRS = Nothing Set MyDB = Nothing End Sub In order for this code to work, we need to create a database and a udl file.

Here is the database. One table is all that is necessary. The necessary fields are shown here.

Page 14: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 14 CP42-1

Create a udl file named “c:\udls\AU2003office.udl” and point it to the access database we just created. Running the macro GetPricingFromWord populates the database. Next, we want to make the database viewable from our company intranet. This involves creating an ASP file to display the database data. We will do this in FrontPage. <% response.expires = 0 %> <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>New Page 1</title> </head> <body> <table border="1" width="700"> <tr> <td width="100">Qty</td> <td width="300">Item</td> <td width="100">Supplier</td> <td width="100">Cost</td> <td width="100">Ext. Cost</td> </tr> <% Dim MyDB Dim MyRS Dim TotCost Set MyDB = Server.CreateObject("ADODB.Connection") Set MyRS = Server.CreateObject("ADODB.Recordset") MyDB.Open "File name=c:\udls\AU2003office.udl" MyRS.Open "Select * from Blocks Order By BSupplier, BName", MyDB, 1, 2 BName = MyRS("BName") BSupplier = MyRS("BSupplier") BlockCnt = 1 BlockCost = MyRS("BCost") While MyRS.EOF = False If MyRS("BName") <> BName Then response.write "<tr>" & vbcr response.write "<td>" & BlockCnt & "</td>" & vbcr response.write "<td>" & BName & "</td>" & vbcr response.write "<td>" & BSupplier & "</td>" & vbcr response.write "<td align=""right"">" & FormatCurrency(BlockCost, 0) & "</td>" & vbcr response.write "<td align=""right"">" & FormatCurrency(BlockCnt * BlockCost, 0) & "</td>" & vbcr TotCost = totcost + BlockCnt * BlockCost response.write "</tr>" & vbcr BlockCost = MyRS("BCost") BName = MyRS("BName") BSupplier = MyRS("BSupplier") Else BlockCnt = BlockCnt + 1 End If MyRS.MoveNext Wend MyRS.Close MyDB.Close Set MyRS = Nothing Set MyDB = Nothing response.write "<tr><td colspan=4><b>Total Cost</td><td align=""right""><b>" & _ FormatCurrency(TotCost, 0) & "</td></tr>" %> </table> </body> </html> That’s the code. Name the file “showcost.asp”. View the file in your web browser now. On my machine, it is “http://puny2/showcost.asp”

Page 15: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 15 CP42-1

This is the result of our ASP file “showcost.asp”. As our Word quotes come in or change, simply re-run the appropriate macro and the web page will automatically update based on the new values.

Page 16: AutoCAD and VBA Integration with Microsoft Office CP42 · PDF fileAutoCAD and VBA Integration with Microsoft Office Autodesk University 2003 —Jerry Winter s CP42-1 Pg. 3 The first

A u t o C A D a n d V B A I n t e g r a t i o n w i t h M i c r o s o f t O f f i c e A u t o d e s k U n i v e r s i t y 2 0 0 3 — J e r r y W i n t e r s

Pg. 16 CP42-1

This concludes the content for this class. If you are reading this but didn’t attend Autodesk University, you missed out! Microsoft Office products can be used to create powerful applications. Why count blocks when AutoCAD can do it? Why enter pricing manually when you can pull it from Word? Once you get your data, making it available to anyone in your organization via your intranet is simple. Feel free to modify the code here to meet your needs. Perhaps 3 additional attributes are not enough. You may want more. We hard-coded some things such as the suppliers we are using. This meets our needs for this presentation but you will probably want to put supplier info in a database. Actually, you probably already have your supplier info in a database. Thanks again for taking the time to attend this class. If you didn’t, I hope to see you next year at Autodesk University 2004!!!! Jerry Winters President, VB CAD [email protected] www.vbcad.com


Recommended