Give this version a try:
(defun c:foo (/ _aap a d l lines p p2 ss text x)
;; RJP » 2021-10-06
(defun _aap (ename pt / param)
(if (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list ename))))
(setq param (vlax-curve-getparamatpoint ename pt))
)
(angle '(0 0) (vlax-curve-getfirstderiv ename param))
)
)
(if (setq ss (ssget '((0 . "*polyline,Line,*Text"))))
(progn
(or (setq d (getdist "\nEnter offset distance:<0> ")) (setq d 0))
(foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
(if (wcmatch (cdr (assoc 0 (entget x))) "*TEXT")
(setq text (cons x text))
(setq lines (cons x lines))
)
)
(if lines
(foreach x text
(setq p (cdr (assoc 10 (entget x))))
(setq
l (mapcar
'(lambda (x)
(list (setq p2 (vlax-curve-getclosestpointto x p)) (distance p p2) (_aap x p2))
)
lines
)
)
(setq l (car (vl-sort l '(lambda (a b) (< (cadr a) (cadr b))))))
;; Check that we have an angle assigned
(if (caddr l)
(progn (entmod (subst (cons 50
((lambda (x)
(setq a (if (<= (* 0.5 pi) x (* 1.5 pi))
(+ x pi)
x
)
)
)
(caddr l)
)
)
(assoc 50 (entget x))
(entget x)
)
)
)
) ; <--- Modified by Jonathan Handojo
;; RJP added offset
(entmod (subst (cons 10 (polar (car l) (+ (/ pi 2) a) d)) (assoc 10 (entget x)) (entget x))
) ; <--- Line added by Jonathan Handojo
)
)
)
)
(princ)
)