乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 164|回复: 15

[编程交流] 将Excel VBA链接到acad

[复制链接]

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:04:41 | 显示全部楼层 |阅读模式
大家好,
我对我的项目印象深刻。如何使用VBA通过Microsoft excel更改Auto cad块属性。请指导使用VBA代码。
示例:Excel在单元格1到10中有一些值,必须通过VBA代码在Auto cad块属性中获取这些值。请尽快回复任何一个
提前谢谢。
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:09:16 | 显示全部楼层
你的意思是所有这些属性都将应用于单个块,即你将在屏幕上被选中,
或者你想将这些值应用于全球许多区块,很高兴知道
本例中的块名称
同时,这个Excel文件是在之前打开的还是您需要在会话中从中打开它
文件对话框?
您的Acad版本和平台是什么?
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:13:22 | 显示全部楼层
这是一个非常基本的例子
  1. Option Explicit
  2. '' Require Reference to:
  3. '' Tools--> References --> Microsoft Excel 1X.0 Type Library
  4. '' and also you have to set options here:
  5. '' Tools--> Options --> Genetral --> Error Trapping -> check 'Break on Unhahdled Errors'
  6. '' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ''
  7. Const xlFileName As String = "C:\Test\TitleBlock.xls" '<-- change data file name
  8. Const sheetName As String = "MyAttributes" '<-- change sheet name
  9. Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
  10. ''------------Late binding example:------------------''
  11. Sub LateBindingExcel()
  12. Dim xlApp As Object
  13. Dim xlBooks As Object
  14. Dim xlBook As Object
  15. Dim xlSheets As Object
  16. Dim xlSheet As Object
  17. Dim xlCells As Object
  18. Dim xlRange As Object
  19. On Error GoTo Err_Control
  20. On Error Resume Next
  21. Set xlApp = Nothing
  22. Set xlApp = GetObject(, "Excel.Application")
  23. If Err.Number <> 0 Then
  24. Set xlApp = CreateObject("Excel.Application")
  25. End If
  26. Err.Clear
  27. Set xlBooks = xlApp.Workbooks
  28. If xlBooks.Count > 0 Then
  29. Set xlBook = xlBooks.Item(1)
  30. End If
  31. If xlBooks.Count = 0 Then
  32. Set xlBook = xlBooks.Open(xlFileName, False)
  33. End If
  34. Set xlSheets = xlBook.worksheets
  35. Set xlSheet = xlSheets.Item(sheetName)  '<--- change a sheet name (might be a sheet number instead)
  36. xlSheet.Application.Visible = True
  37. Set xlCells = xlSheet.Cells
  38. Set xlRange = xlCells.Range("$A1:$A10")
  39. Dim i As Long
  40. i = 1
  41. Dim strAddr As String
  42. Dim attrData(0 To 9) As String
  43. For i = 1 To 10
  44. strAddr = "A" & CStr(i)
  45. Set xlRange = xlCells.Range(strAddr)
  46. Dim cellVal
  47. cellVal = xlRange.Value
  48. attrData(i - 1) = CStr(cellVal) '<-- store data you took from Excel for the future use
  49. Next
  50. ThisDrawing.Activate
  51. ' keep this order
  52. Set xlRange = Nothing
  53. Set xlCells = Nothing
  54. Set xlSheet = Nothing
  55. Set xlSheets = Nothing
  56. xlBook.Close False
  57. Set xlBook = Nothing
  58. Set xlBooks = Nothing
  59. xlApp.Quit
  60. Set xlApp = Nothing
  61. MsgBox "Select blocks"
  62. ''------------- End of Excel work-----------------''
  63. Dim oEnt As AcadEntity
  64. Dim oBlkRef As AcadBlockReference
  65. Dim oAttrib As AcadAttributeReference
  66. Dim attVal  As Variant
  67. Dim ftype(0 To 1) As Integer
  68. Dim fdata(0 To 1) As Variant
  69. Dim dxfCode, dxfValue
  70. ftype(0) = 0: fdata(0) = "insert"
  71. ftype(1) = 66: fdata(1) = 1
  72. dxfCode = ftype: dxfValue = fdata
  73.     Dim oSset As AcadSelectionSet
  74.          With ThisDrawing.SelectionSets
  75.               While .Count > 0
  76.                    .Item(0).Delete
  77.               Wend
  78.          Set oSset = .Add("$MyBlocks$")
  79.          End With
  80. oSset.SelectOnScreen dxfCode, dxfValue
  81. If oSset.Count = 0 Then
  82. MsgBox "nothing selected, exit..."
  83. Exit Sub
  84. End If
  85. For Each oEnt In oSset
  86. Set oBlkRef = oEnt
  87. attVal = oBlkRef.GetAttributes
  88. For i = LBound(attrData) To (UBound(attrData)) '<-- assuming a contents size of the stored data
  89. Set oAttrib = attVal(i)
  90. If attrData(i) <> vbNullString Then
  91. oAttrib.TextString = attrData(i)
  92. End If
  93. Next i
  94. Next oEnt
  95. Err_Control:
  96. If Err.Number <> 0 Then
  97. MsgBox Err.Description
  98. End If
  99. End Sub
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:20:08 | 显示全部楼层
你好
我想将excel单元格值链接到autocad属性块。运行VBA时,excel值必须更新块内属性值。块属性名称是常量,唯一的变量是excel值。我有一个在excel中打开acad的代码。
但我只需要这个选项的代码。请任何人帮我通过任何代码。
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:21:04 | 显示全部楼层
尊敬的Fixo:,
我对这个很陌生。HANDLETOOBJECT是什么。
我的问题是excel单元格中有10个变量值(即:10个单元格)。&我在acad中有恒定的10个块属性。
在运行VBA时,这些excel文本必须粘贴到10块属性值的acad文本中。
先生,请帮我接通密码。
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:27:40 | 显示全部楼层
块名称和属性标记是什么?
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:32:14 | 显示全部楼层
块名称是一个,属性标记是结构,只有块值必须更新
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:35:58 | 显示全部楼层
我合并了你所有的线程。
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:39:50 | 显示全部楼层
我不能把你的作业做完,
最好查看帮助文件,文档,搜索所需信息
在本论坛和其他论坛上
添加您需要自己筛选的块名,
这是我在这个帖子中的最后一篇帖子
  1. dim obj as AcadEntity
  2. Dim oText as AcadText
  3. dim handlerange as range
  4. dim textrange as range
  5. set handlerange=mySheet.Range("A1000")
  6. set textrange=mySheet.Range("B1000")
  7. Set obj= thisdrawing.HandleToObject(range.Value)
  8. set txtobj= obj
  9. txtobj.TextString= textrange.value''--> or value2

 
~'J'~
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:43:22 | 显示全部楼层
尊敬的fixo:,
 
 
你能发布你的VBAExcelAutoCAD吗。请再说一遍?链接现在已断开。
 
 
当做
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 11:44 , Processed in 0.336548 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表