2016-03-03 137 views
-2

当我尝试在Autocad中运行此VBA代码时,出现“类型不匹配”错误。突出显示行Theatts(TagNumber).TextString = BTextStringVBA中的类型不匹配

Public acad As Object 
Public doc As Object 
Public ms As Object 
Public ss As Object 
Public ssnew As Object 
Public Theatts As Variant 
Public MsgBoxResp As Integer 

'declare global variables 

Sub UpdateAttrib(TagNumber As Integer, BTextString As String) 

    'This Sub Procedure tests the attribute data to check 
    'that is not a null value 

    If BTextString = "" Then 
    'if the attribute is empty 

    Theatts(TagNumber).TextString = "" 
    'put a '-' place holder 

    Else 
    'if it is not empty 

    Theatts(TagNumber).TextString = BTextString 
    'use the attribute value 

    End If 

End Sub 

Sub setupsv() 
'name of function 
UserForm1.show 
'display the dialogue box 
'UserForm1 
End Sub 
+1

什么是Theatts应该是?它看起来完全是单元化 –

+0

格式化您的代码将有助于... – Bart

+0

@约翰科尔曼:Theatts的声明是在他的代码的无格式部分... – Bart

回答

0

Theatts不是String阵列,它是一个Variant。 使用Redim来初始化它或将其声明为String数组(或者如果坚持的话,则为Variant的数组)。

+0

OP似乎将它视为一个数组或一些未指定类型的对象的集合,它拥有一个字符串属性而不是一个字符串本身的数组。 –

+2

这样的事情属于评论。你没有足够的上下文进行随机猜测。 OP没有给出足够的上下文来允许任何明确的答案。 –

1

我修改了代码,不再有“类型不匹配”的问题。显然,这是一个简单的程序,但我打算逐渐建立它。

Public acad As Object 
Public doc As Object 
Public ms As Object 
Public ss As Object 
Public ssnew As Object 
Public Theatts As Variant 
Public MsgBoxResp As Integer 

Private Sub CommandButton1_Click() 
UpdateAttrib 0, UserForm1.Txt1 
End Sub 

Private Sub UserForm_Initialize() 
Dim BlkG(0) As Integer 
Dim TheBlock(0) As Variant 
Dim Pt1(0 To 2) As Double 
Dim Pt2(0 To 2) As Double 
'declare local variables 

Set acad = GetObject(, "AutoCAD.Application") 
'set reference to AutoCAD 

Set doc = acad.ActiveDocument 
'set reference to the drawing 

Set ms = doc.ModelSpace 
'set reference to model space 

Set ssnew = doc.SelectionSets.Add("TBLK") 
'create a selection set 

Pt1(0) = 0: Pt1(1) = 0: Pt1(2) = 0 
Pt2(0) = 3: Pt2(1) = 3: Pt2(2) = 0 
'set up the array 

BlkG(0) = 2 
'group code 2 for block name 

TheBlock(0) = "SV-PCS7" 
'the name of the attribute block 

ssnew.Select 5, Pt1, Pt2, BlkG, TheBlock 
'get the block 

If ssnew.Count >= 1 Then 
'if the block is found 

Theatts = ssnew.Item(0).GetAttributes 
'get the attributes 

UserForm1.Txt1.Text = UCase(LTrim(Theatts(0).TextString)) 
'get the title attribute 
'clear any leading spaces and 
'convert to uppercase 

UserForm1.Txt1.Text = UCase(LTrim(Theatts(1).TextString)) 

UserForm1.Txt1.SetFocus 
UserForm1.Txt1.SelStart = 0 
UserForm1.Txt1.SelLength = Len(UserForm1.Txt1.Text) 
'set the focus to the drawing title and highlight it 

Else 
    'if no attribute title block is found 

MsgBox "Sorry - No Material List Attributes....", vbCritical 
'inform the user that there is no attribute title block 

ThisDrawing.SelectionSets("TBLK").Delete 

End 
'end the application 

End If 

ThisDrawing.SelectionSets("TBLK").Delete 

End Sub 
'declare global variables 

Sub UpdateAttrib(TagNumber As Integer, BTextString As String) 

'This Sub Procedure tests the attribute data to check 
'that is not a null value 

If BTextString = "" Then 
'if the attribute is empty 

Theatts(TagNumber).TextString = "" 
'put a '-' place holder 

Else 
'if it is not empty 

    Theatts(TagNumber).TextString = BTextString 
    'use the attribute value 

    End If 

End Sub 

Sub setupsv() 
'name of function 
UserForm1.show 
'display the dialogue box 
'UserForm1 
End Sub