Free VBA code snippets for AutoCAD
Please feel free to be inspired, cut&paste or if you have any feedback or questions go here. If you want some customization, VBA conversion to .NET or anything else that you can come up with that we might help you with you're welcome to contact us.
UPDATE: As of January 31, 2014, Autodesk is no longer authorized to distribute VBA 6 or earlier versions of VBA for use with Autodesk AutoCAD and other Autodesk products. This change affects the availability to download and install VBA for Autodesk AutoCAD 2013 or earlier.
When you load a DVB file that is already loaded you can get this message:
File already loaded: C:\yourproject.dvb
AutoCAD Focus Control for VBA Type Library (AcFocusCtrl16.dll or AcFocusCtrl.dll) found in C:\Program Files\Common Files\Autodesk Shared is useful if you have a modeless form in the VBA project. Without it the focus does not remain at a modeless form (ShowModal property set to false, available in AutoCAD 2002 and newer versions). Use the Tools menu and add a reference to the AcFocusCtrl16.dll file and then add the AcFocusCtrl by right-clicking for additional controls in the VBAIDE toolbox when you have the form in edit mode. In some cases it can be needed to register the dll file using regsvr32.
Sample to write and read Xrecord values in a Dictionary:
Public Sub WriteXRec() Dim oDict As AcadDictionary Dim oXRec As AcadXRecord Dim dxfCode(0 To 1) As Integer Dim dxfData(0 To 1) Set oDict = ThisDrawing.Dictionaries.Add("SampleTest") Set oXRec = oDict.AddXRecord("Record1") dxfCode(0) = 1: dxfData(0) = "First Value" dxfCode(1) = 2: dxfData(1) = "Second Value" oXRec.SetXRecordData dxfCode, dxfData End Sub Public Sub ReadXRec() Dim oDict As AcadDictionary Dim oXRec As AcadXRecord Dim dxfCode, dxfData Set oDict = ThisDrawing.Dictionaries.Item("SampleTest") Set oXRec = oDict.Item("Record1") oXRec.GetXRecordData dxfCode, dxfData Debug.Print dxfData(0) Debug.Print dxfData(1) End Sub