Updating my version to include Mtext.
;; Example by CAB
;; Moves Text or Mtext to center while justifying Middle Center
;; Changes Mtest to Width Zero
(defun c:CenterText (/ ss i elast obj bent ipt prop)
(defun getmid (ent)
(setq lst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) (entget ent))))
(if (= (length lst) 4)
(mapcar '(lambda (a b) (/ (+ a b) 2.)) (car lst) (caddr lst))
)
)
(command "_undo" "begin")
(prompt "\Select text within the table.")
(if (setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
(progn
(setq i -1)
(while (setq ename (ssname ss (setq i (1+ i))))
(setq obj (vlax-ename->vla-object ename))
(if (eq "AcDbText" (vla-get-ObjectName obj))
(setq prop 'TextAlignmentPoint)
(setq prop 'insertionpoint)
)
(setq elast (entlast))
(setq ipt (vlax-get obj 'insertionpoint))
(if (vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(vl-cmdf "_.-boundary" "_a" "_i" "_n" "" "" ipt "")
(while (> (getvar "CMDACTIVE") 0) (command ""))
)))
(princ "\nText boundry not found.")
(progn ; got a boundry
(if (and (not (eq elast (setq bent (entlast))))
(setq midpt (getmid bent))
)
(if (eq "AcDbText" (vla-get-ObjectName obj))
(progn
(vla-put-Alignment obj acAlignmentMiddleCenter)
(vla-put-TextAlignmentPoint Obj (vlax-3D-point MidPT))
)
(progn
(vla-put-Width obj 0.0)
(vla-put-AttachmentPoint obj acAttachmentPointMiddleCenter)
(vla-put-insertionpoint Obj (vlax-3D-point MidPT))
)
)
)
(and (eq bent (entlast)) (entdel bent))
)
)
)
)
)
(command "_undo" "end")
(princ)
)