2016-03-03 138 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