Hi Marcelo,
to get the 'FIELDCODE' from TEXT or MTEXT, it is easy, we can get the 'FIELDCODE' property from TEXT or MTEXT as a vla-object.
(defun c:demo ( / hnd obj)
(if (and (setq hnd (car (nentsel "\Select a Field to add \"\/301\": ")))
(setq obj (vlax-ename->vla-object hnd))
(vlax-method-applicable-p obj 'FIELDCODE)
(vlax-write-enabled-p obj)
)
(vla-put-textstring obj (strcat (vlax-invoke-method obj 'FIELDCODE) "\/301"))
(prompt "\nNot a field, or method not applicable... ")
)
(princ)
)
But to get the 'FIELDCODE' from an ATRRIB it is not easy, we need to step thru dictionaries and get the FIELD, then get all 360's pairs, long fieldcodes have multiple 360's pairs, get the ObjectID and put everything together in one 'FIELDCODE' string.
Sorry for my rough explanation, but English is not my native language...
The following quick and dirty 'demo' was mostly borrowed from Tlindell's 'fieldtotext', and it's just to show you one way to access the 'FIELDCODE' from an ATRRIB...
; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/getting-the-field-code-of-text-strings/m-p/1459283#M193818
; From 'fieldtotext' by Tlindell
(defun c:demo (/ ent entid fld Counter fldNo fldList obj tmp subFldTxt subFldOid)
(if (and (setq ent (car (nentsel "\Select a Field to add \"\/301\": ")))
(setq entid (entget ent))
(wcmatch (cdr (assoc 0 entid)) "MTEXT,TEXT,ATTRIB")
(assoc 360 entid)
(assoc 360 (entget (cdr (assoc 360 entid))))
(setq obj (vlax-ename->vla-object ent))
(vlax-write-enabled-p obj)
)
(progn
(setq fld (entget (cdr (assoc 360 (entget (cdr (assoc 360 (entget (cdr (assoc 360 entid)))))))))
txt (cdr (assoc 2 fld))
cntr 0
fldNo (cdr (assoc 90 fld))
fldList (vl-remove-if-not '(lambda (x) (= (car x) 360)) fld)
)
(while (< cntr fldNo)
(setq tmp (strcat "\\_FldIdx " (itoa cntr))
subFldTxt (cdr (assoc 2 (entget (cdr (nth cntr fldList)))))
)
(if (assoc 331 (entget (cdr (nth cntr fldList))))
(progn
(setq subFldOid (strcat "ObjId " (itoa (vla-get-ObjectID (vlax-ename->vla-object (cdr (assoc 331 (entget (cdr (nth cntr fldList))))))))))
(setq subFldTxt (vl-string-subst subFldOid "ObjIdx 0" subFldTxt))
)
)
(setq txt (vl-string-subst subFldTxt tmp txt))
(setq cntr (+ cntr 1))
)
(vla-put-textstring obj (strcat txt "\/301"))
)
(prompt "\nNot a field... ")
)
(princ)
)
Hope this helps,
Henrique