-2

I am getting the error "type mismatch" when I try to run this VBA code in Autocad. The line Theatts(TagNumber).TextString = BTextString is highlighted.

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
John Coleman
  • 51,337
  • 7
  • 54
  • 119
tmccar
  • 85
  • 11
  • 1
    What is `Theatts` supposed to be? It seems completely unitialized – John Coleman Mar 03 '16 at 15:57
  • Formatting your code properly would help... – Bart Mar 03 '16 at 15:57
  • @John Coleman : the declaration of Theatts is in the unformatted part of his code... – Bart Mar 03 '16 at 16:00
  • 1
    @Bart The *declaration* is there -- but not anything which would cause it to have any nonempty value, hence it is unitialized. My guess is that it is a global variable whose initialization OP hasn't shown. They seem to be trying to treat it as an array of objects of some type. – John Coleman Mar 03 '16 at 16:01
  • You are showing a fragment of your code. The problem is likely to be in code that you haven't shown. Psychic debugging is rather hard. – John Coleman Mar 03 '16 at 16:10
  • Yes, it's a fragment but I am debugging as I go. I have not coded the full program yet and I am stuck at this point. – tmccar Mar 03 '16 at 16:27
  • 1
    But your fragment doesn't run at all since there isn't any code which *calls* your sub. How is `Theatts` supposed to get any value at all? Surely there must be some code in the calling sequence to give it a value. Otherwise I am mystified as to why you would expect your code to do anything other than crash and burn. – John Coleman Mar 03 '16 at 16:41
  • OP, please show us code, where you set the `Theatts` as array, for example `ReDim Theatts(10)` and the calling line for `Sub UpdateAttrib(TagNumber As Integer, BTextString As String)` – kolcinx Mar 03 '16 at 16:50
  • Start with `Option Strict`. `Theatts` is an array of `AttributeReference`. Strongly typed code will take a lot of the guesswork out of this. – CAD bloke Mar 21 '16 at 03:27

2 Answers2

1

I have revised the code and no longer have the issue with "type mismatch". Obviously, it's a simple program but I plan to build on it gradually.

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
TylerH
  • 20,799
  • 66
  • 75
  • 101
tmccar
  • 85
  • 11
0

Theatts is not an array of String, it is a Variant. Use Redim to initialize it or declare it as array of String (or array of Variant if you insist on that).

Bart
  • 547
  • 3
  • 16
  • OP seems to be treating it as an array or collection of objects of some unspecified type which posses a string attribute rather than an array of strings per se. – John Coleman Mar 03 '16 at 16:04
  • 2
    Things like this belong in the comments. You are making random guesses without enough context. OP hasn't given enough context to allow for any definitive answer. – John Coleman Mar 03 '16 at 16:07