Your code came pretty close...
These are the adjustments I made:
1. Added following 2 sub functions to get & then set dynamic block visibility states property written by @komondormrex
; add the following two sub functions
; by komondormrex
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/swap-the-visibility-state-in-a-block-using-lisp/m-p/12786544/highlight/true#M466339
(defun get_dyn_property_by_name (dyn_property_name insert_object / dyn_property_found)
(if (vl-some '(lambda (dyn_property) (= dyn_property_name (vla-get-propertyname (setq dyn_property_found dyn_property))))
(vlax-invoke insert_object 'getdynamicblockproperties)
)
dyn_property_found
nil
)
)
; modified version:
(defun set_dyn_property (dyn_property_name value obj / stamp_property)
(if (setq stamp_property (get_dyn_property_by_name dyn_property_name obj))
(vla-put-value stamp_property value)
)
)
2. Modified your initget statements since each selection must be a single word without spaces or underscores:
;; Prompt user for visibility state
; (initget "TO PAGE FROM PAGE")
; (setq visState (getkword "\nEnter visibility state [TO PAGE/FROM PAGE]: "))
(initget "TO-PAGE FROM-PAGE") ; use hyphen to preserve each choice as a single item - note underscore not supported
(setq visState (getkword "\nEnter visibility state [TO-PAGE/FROM-PAGE]: "))
; add this
(if (equal visState "TO-PAGE") (setq visState "TO PAGE")(setq visState "FROM PAGE")) ; force to set to a correct vis state name
;
3. Apply the set_dyn_property subfunction:
(if (or (equal visState "TO PAGE") (equal visState "FROM PAGE"))
(progn
;(vla-put-EffectiveName (vlax-ename->vla-object ent) visState)
(set_dyn_property "Visibility1" visState (vlax-ename->vla-object ent)) ; set to the Visibility1 paramenter
4. Before you set Defpoints as current layer:
;; Ensure the layer exists, if not, create it
(if (not (tblsearch "layer" "DEFPOINTS"))
(command "._LAYER" "M" "DEFPOINTS" "C" "7" "" "")
)
5. Add (princ) at end to remove double echo:
(princ "\nType PFA to run the command.")(princ) ; add princ at end to prevent double echo
6. When finish troubleshooting then localize variables:
(defun c:PFA (/ get_dyn_property_by_name set_dyn_property blockName layerName blockDef ent visState) ; localize variables