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