vba实现CAD块属性导出到excel中

news/2024/7/5 4:06:59

vba实现CAD与excel交互功能可提高工作效率,此例可供参考。

vba6运行程序前需在vba ide中工具栏下引用选项中引用excel库方可运行,vba7可直接运行。

代码如下:

Sub 导出CAD块属性到excel()
    Dim Excel As Object
    Dim elem As Object
    Dim excelSheet As Object
    Dim Array1 As Variant
    Dim Count, RowNum As Integer
    Dim NumberOfAttributes As Integer
    
    ' Start Excel
    On Error Resume Next
    
    Set Excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set Excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "不能加载excel", vbExclamation
            End
        End If
    End If
    
    On Error GoTo 0
    
    Excel.Visible = True
    Excel.Workbooks.Add
    Excel.Sheets("Sheet1").Select
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Sheet1")
    
    RowNum = 1
    Dim Header As Boolean
    For Each elem In ThisDrawing.ModelSpace
        If StrComp(elem.EntityName, "AcDbBlockReference", 1) = 0 Then
            If elem.HasAttributes Then
                Array1 = elem.GetAttributes
                For Count = LBound(Array1) To UBound(Array1)
                    If Header = False Then
                        If StrComp(Array1(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                            excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TagString
                        End If
                    End If
                Next Count
                RowNum = RowNum + 1
                For Count = LBound(Array1) To UBound(Array1)
                    excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TextString
                Next Count
                Header = True
            End If
        End If
    Next elem
    
    NumberOfAttributes = RowNum - 1
   
    If NumberOfAttributes > 0 Then
        excelSheet.Range(Cells(1, 1), Cells(1, 100)).Font.Bold = True
        
        'For a specific set of attribute information this could
        'be set to fit the exact number of columns.
        excelSheet.Columns("A:G").AutoFit
        'Rename the worksheet
        Sheets("Sheet1").Name = "Attributes"
        If Chart.Value = True Then
            CreateChart (NumberOfAttributes)
        End If
        If Memo.Value = True Then
            MakeMemos
        End If
    Else
        MsgBox "当前图中未找到属性", vbInformation
        Excel.Quit
    End If

End Sub


http://lihuaxi.xjx100.cn/news/1892311.html

相关文章

利用闭包与高阶函数实现缓存函数的创建

缓存函数是一种用于存储和重复利用计算结果的机制。其基本思想是&#xff0c;当一个函数被调用并计算出结果时&#xff0c;将该结果存储在某种数据结构中 (通常是一个缓存对象)以备将来使用。当相同的输入参数再次传递给函数时&#xff0c;不再执行实际的计算&#xff0c;而是直…

微服务学习:Gateway服务网关

一&#xff0c;Gateway服务网关的作用&#xff1a; 路由请求&#xff1a;Gateway服务网关可以根据请求的URL或其他标识符将请求路由到特定的微服务。 负载均衡&#xff1a;Gateway服务网关可以通过负载均衡算法分配请求到多个实例中&#xff0c;从而平衡各个微服务的负载压力。…

利用Pytorch预训练模型进行图像分类

Use Pre-trained models for Image Classification. # This post is rectified on the base of https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/# And we have re-orginaized the code script.预训练模型(Pre-trained models)…

PHP的协程是什么?

PHP 的协程是一种轻量级的线程&#xff08;或任务&#xff09;实现&#xff0c;允许在一个进程中同时执行多个协程&#xff0c;但在任意时刻只有一个协程处于执行状态。协程可以看作是一种用户空间线程&#xff0c;由程序员显式地管理&#xff0c;而不是由操作系统内核进行调度…

KVO KVC

KVO & KVC KVC KVC&#xff08;Key-value coding&#xff09;键值编码&#xff0c;就是指iOS的开发中&#xff0c;可以允许开发者通过Key名直接访问对象的属性&#xff0c;或者给对象的属性赋值。而不需要调用明确的存取方法。这样就可以在运行时动态地访问和修改对象的属…

场景的组织及渲染(二)

3.11 分页细节层次节点 分页细节层次节点(osg&#xff1a;&#xff1a;PagedLOD)继承自osg::LOD 节点&#xff0c;它也是一个细节层次节点&#xff0c;用于实现动态分页加载&#xff0c;根据视点来加载所需要的,分页细节层次节点中还可以包含LOD节点。它与osg::LOD节点的区别是…

如何在Ubuntu的Linux系统上搭建nacos集群

官方给出的集群部署架构图 集群部署说明 (nacos.io)3个或3个以上nacos节点才能构成集群当前示例中包含3个nacos节点&#xff0c;同时一个负载均衡器代理3个nacos&#xff0c;本示例中负载均衡器可使用的是nginx 准备并安装好正常运行的nginx&#xff0c;本示例略准备并安装好正…

【MySQL学习】概述

文章目录 1. mysql的启动和停止命令2. 客户端连接3. 数据模型 1. mysql的启动和停止命令 通过指令启动或停止&#xff0c;以管理员身份运行cmd&#xff0c;进入命令行执行如下指令&#xff1a; &#xff08;1&#xff09;启动myaql net start mysql&#xff08;2&#xff09;…