Invalid argument tag - vba

I am doing some customization of block insertion in Autocad. When setting up the attributes, I am getting an error in my procedure:
"Invalid argument Tag in setting TagString"
The code is as follows:
Sub Ch10_GettingAttributes()
' Create the block
Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
insertionPnt(0) = 0
insertionPnt(1) = 0
insertionPnt(2) = 0
Set blockObj = ThisDrawing.Blocks.Add _
(insertionPnt, "TESTBLOCK")
' Define the attribute definition
Dim attributeObj As AcadAttribute
Dim height As Double
Dim mode As Long
Dim prompt As String
Dim insertionPoint(0 To 2) As Double
Dim tag As String
Dim value As String
height = 1#
mode = acAttributeModeVerify
prompt = "Attribute Prompt"
insertionPoint(0) = 5
insertionPoint(1) = 5
insertionPoint(2) = 0
tag = "Attribute Tag"
value = "Attribute Value"
' Create the attribute definition object on the block
Set attributeObj = blockObj.AddAttribute(height, mode,_
prompt, insertionPoint, tag, value)
End Sub
What would cause this error?

Well, the error message is rather explicit, you have an "Invalid argument Tag in setting TagString"
So simply get rid of space in the tag value and you should be good to go. It doesn't support spaces.
tag = "Attribute Tag" 'BAD
tag = "AttributeTag" 'OK

Related

Why do I get an 'Object is referenced' error when deleting a block by name?

I'm creating some lines dynamically from an Excel file.
I want to delete them programmatically; I created them as a block with a custom name (I don't know! Maybe there is another way).
Now, I want to delete a block by name:
Dim Blk As AcadBlock
Set Blk = ThisDrawing.Blocks("1|3137.34101634953|22059.6018301436|13369.4589738571|18917.7435065279||line")
Blk.Delete
But I got this error:
Object is referenced
this is my function to create line as block:
Function CreateLineBlock(firstPoint, secondPoint, bname)
Dim StartPoint(0 To 2) As Double, insPt(0 To 2) As Double
Dim MyBlock As AcadBlock
Dim EndPoint(0 To 2) As Double
Dim BlockName As String
BlockName = bname & "|line"
StartPoint(0) = firstPoint(0)
StartPoint(1) = firstPoint(1)
'StartPoint(2) = 0
EndPoint(0) = secondPoint(0)
EndPoint(1) = secondPoint(1)
'EndPoint(2) = 0
'With ThisDrawing.ModelSpace
'.AddLine StartPoint, EndPoint
'.Item(.Count - 1).Update
'End With
'Create a new block
Set MyBlock = ThisDrawing.Blocks.Add(insPt, BlockName)
'ADD LINES DIRECT ON YOUR BLOCK
MyBlock.AddLine StartPoint, EndPoint
'Add the blockreference in model space
ThisDrawing.ModelSpace.InsertBlock insPt, BlockName, 1, 1, 1, 0
End Function
How can I delete this block?
Per the method demonstrated by my Delete Blocks function, you'll first need to delete all references of the block definition from all containers (i.e. all other block definitions and layouts) before the block definition of the target block can be deleted.
In VBA, this might looks something like this:
Function DelBlock(strBnm As String)
Dim objDef As AcadBlock
Dim objCmp As AcadObject
For Each objDef In ThisDrawing.Blocks
If Not objDef.IsXRef Then
For Each objCmp In objDef
If objCmp.ObjectName = "AcDbBlockReference" Then
If objCmp.Name = strBnm Then
objCmp.Delete
End If
End If
Next objCmp
End If
Next objDef
ThisDrawing.Blocks(strBnm).Delete
End Function

Argument not optional, how can I change it?

I'm getting a message Argument not optional running the code below:
Private Sub CommandButton1_Click()
Dim linia As AcadLine
Dim Pt1(0 To 2) As Double
Dim stopien(0 To 3) As Double
Dim segment As Variant
Dim n As Double
Dim h As Double
n = CDbl(TextBox1)
h = CDbl(TextBox2)
Pt1(0) = 10# '
Pt1(1) = 10# '
Pt1(2) = 0# '
segment = Pt1
For Licznik = 0 To n - 1
stopien(0) = segment(0)
stopien(1) = segment(1)
stopien(2) = segment(0) + h
stopien(3) = segment(1)
Set linia = ThisDrawing.ModelSpace.AddLine(stopien)
segment(0) = stopien(2)
segment(1) = stopien(3)
Next
End Sub
I can debug it , but if I write something in the TextBox1 or TextBox2 I get this message.
I don't know how I can fix it
The error message is telling you exactly what the issue is... namely, that you are not supplying a needed argument. In this case, the offending line is below:
Set linia = ThisDrawing.ModelSpace.AddLine(stopien)
The AddLine method takes both a StartPoint and an EndPoint, and you are only supplying one of them.
CDbl to the textbox.. hmm.. Shouldn't that be Textbox1.value ? I think you need more uh error handling on the input for Textbox1 and Textbox2.
For example do something like this before you start converting - so put a breakpoint on your first value set of n = CDbl(Textbox1). Then make sure you can see the debug / immediate views in VBA editor. Then write something like this or add Textbox1 to a watch, and expand it's properties to make sure you are using the object's values correctly.
Debug.print(Textbox1)
Debug.print(Textbox1.value)
To handle problems in conversion or w/e, you want to add error handles to the value checks before you set the values to variables.
MrExcel classic discussion on userform handling input errors

CATIA VBA Measure a user selected line(s)/spline

I am trying to get the length of user selected lines/splines
This is the code I'm using to have users select their lines:
Dim USel As Selection
Dim USelLB
Dim InputObject(0)
InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
USel.Clear
USelLB.Clear
Linestomeasure = USelLB.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
Linestomeasure is a public variable, in the mainsub i've been trying to measure Linestomeasure using the following code:
Dim pd1 As PartDocument
Dim a As Object
Dim c As Reference
a = TrimLines.Item(1)
c = pd1.Part.CreateReferenceFromObject(a)
Dim Mea1 As Measurable
Dim TheSPAWorkbench As SPAWorkbench
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Set Mea1 = TheSPAWorkbench.GetMeasurable(c)
But when I run the code a = trimLines.Item(1) gets highlighted in the debugger with the error message "Object Required".
Does anyone have an idea on how I can change my code so that I can get the length of the line as a variable that I can work with ? Or just a different way to go about what I'm trying to do?
Edited answer to reflect comment bellow
Looks like you are assigning the wrong type of variable to the USelLB.SelectElement3 and also missunderstanding how it actually works.
The Selection.SelectElement3 returns a String that reflects whether the selection was sucessfull or not.
The Object retrieved from the Selection is inside the Selection.Item(Index)
Your code should be something like this:
Dim PD1 as PartDocument
Dim Sel 'as Selection 'Sometimes it is needed to comment the selection to use the .SelectElement3 method
Dim InputObjType(0)
Dim SelectionResult as string
Dim LineToMeasure as AnyObject
Dim I as Integer
Dim SpaWorkbench as SPAWorkbench
Dim Measurable as Measurable
InputObjType(0) = "AnyObject"
'set PD1 = Catia.ActiveDocument
set Sel = PD1.Selection
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Sel.Clear
SelectionResult= Sel.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
If SelectionResult = "Ok" or SelectionResult = "Normal" then 'Check if user did not cancel the Selection
For i = 1 to Selection.Count
Set LineToMeasure = Sel.Item(i).Value
set Measurable = SpaWorkbench.GetMeasurable(LineToMeasure)
'Measure whatever you need here.
Next
End If
Keep in mind that using the AnyObject type filter may cause the user to select unwanted objects. You shoudl use a more specific filter.

How can I convert an Userform Textbox value which is a String into a Long in Excel Vba?

Here I am trying to read a Textbox.Value which is a String into a Variable (i.e. text_bval) and converting that String into a Long using CLng function for my calculations. The CLng function is showing run time error like type mismatch.
See my code below:
Dim text_bval As String
Dim text_long As Long
Dim per As Integer
text_bval = text_box.value
user_entry = CLng(text_bval)
According to the MS Documentation, "If expression lies outside the acceptable range for the Long subtype, an error occurs."
In this case, your expression is text_bval, a string. If your string contains characters that cannot be converted to a number, an error will occur.
For example:
text_bval = "2" then CLng(text_bval) = 2
text_bval = "2.3" then CLng(text_bval) = 2
text_bval = "hello" then CLng(text_bval) = ERROR
In order to prevent an error from occuring, you should do something like the following:
user_entry = 0
On Error Resume Next
user_entry = CLng(text_bval)
On Error GoTo 0

Evaluate("1") gives error 438

I have few "Why?"s about evaluate...
Sub TestEvaluate()
Dim Tag As String
Tag = "5"
Tag = Evaluate(Tag) 'works fine
Tag = "1"
Tag = Evaluate(Tag) 'error 438 wrong property or method(-> my 1st "Why?")
But ok i can handle it:
Tag = "1"
Tag = [Tag] 'works fine
Now I need to evaluate a property of some object:
Dim Object As cObject
Set Object = New cObject
Object.pProperty = "5"
Tag = Evaluate(Object.pProperty) 'Works fine
And again the same problem as above:
Object.pProperty = "1"
Tag = Evaluate(Object.pProperty) '438 wrong property or method
But now i'm traped, becouse:
Tag = [Object.pProperty] 'generates error 13 type mismatch(-> my 2nds "Why?")
Is there some solution without the need to use a new variable?
Dim TempTag As String
TempTag = Object.pProperty
Tag = [TempTag] 'everything fine again
End Sub
i found out, in my case VBA.Evaluate("1") generates an object according to
debug.print VBA.VarType(evauate("1"))
It`s just a bug? (win8.1 xl2007)
I put the expression into brackets and the problem disappeared; works fine now:
Tag = Evaluate("(" & Tag & ")")
This is better solution for me:
Tag = Evaluate(Tag & "+0")
It is a solution for the Error 2015 when the Tag="" as well.