英雄无敌能向你请教个问题吗?
英雄无敌函数“TextString”并非在所有代码路径上都返回值。当使用结果时,可能会在运行时发生空引用异常。
Dim textObj As AcadText
textObj = ThisDrawing.ModelSpace.Item(0)
text = textObj.TextString
MsgBox("The TextString property equals: " & text, vbInformation, "TextString 示例")
取不到好像取不到对象!
Dim textObj As AcadEntity
Dim text1 As AcadText
Dim str As String
set textObj = ThisDrawing.ModelSpace.Item(0)
If textObj.ObjectName = "AcDbText" Then
Set text1 = textObj
str = text1.TextString
MsgBox("The TextString property equals: " & str, vbInformation, "TextString 示例")
End If
就像2楼说的一样,你模型空间中的第0个图元不一定是一个文字。 Dim i As Integer
Dim textObj As AcadEntity
Dim text1 As AcadText
Dim str As String
Dim count As Integer
count = ThisDrawing.ModelSpace.Count
Try
For i = 0 To i = count - 1
textObj = ThisDrawing.ModelSpace.Item(i)
If textObj.ObjectName = "AcDbText" Then
text1 = textObj
str = text1.TextString
MsgBox("The TextString property equals: " & str, vbInformation, "TextString 示例")
End If
Next
Catch
MsgBox("出错")
End Try
我都改成这样了,可是还是抛出异常!
我不知道你的代码是怎么来的,try是什么?cathc又是什么?
把你的代码改了一下:
Sub test()
Dim i As Integer
Dim textObj As AcadEntity
Dim text1 As AcadText
Dim str As String
Dim count As Integer
count = ThisDrawing.ModelSpace.count
For i = 0 To count - 1
Set textObj = ThisDrawing.ModelSpace.Item(i)
If textObj.ObjectName = "AcDbText" Then
Set text1 = textObj
str = text1.TextString
MsgBox "The TextString property equals: " & str, vbInformation, "TextString 示例"
End If
Next
End Sub
try是什么?cathch又是什么?是捕捉异常
我运行 会抛出异常!Catch
MsgBox("出错") Dim entry As AcadEntity
Dim entObjectID As Long
For Each entry In ThisDrawing.ModelSpace
MsgBox(entry.ObjectName.ToString())
entObjectID = entry.ObjectID
entry.Highlight(True)
MsgBox("The ObjectID of this object is " & entObjectID, vbInformation, "ObjectID 示例")
entry.Highlight(False)
Next
根本无法运行到FOR循环里面!
我刚测试后发现,我根本无法得到模型空间的图元对象。运行这段代码得到的结果是There are no objects in model space。
If ThisDrawing.ModelSpace.Count0 Then
entry = ThisDrawing.ModelSpace.Item(0)
MsgBox(entry.ObjectName + _
" is the first entity in model space.")
Else
MsgBox("There are no objects in model space.")
End If
后来我发现了问题的所在,ThisDrawing = acadapp.ActiveDocument 得到的是当前被打开的活动文档,如果文档没有被打开,是无法得到的,现在请问有什么办法在不用CAD打开这个DWG文件的情况下,得到这个DWG文件的文档吗?
搞清楚VBA、VB、VB.net
try cath end try是VB.net中的错误处理
页:
[1]