need help to modify the text replacement lisp file

need help to modify the text replacement lisp file

kwankitfu
Participant Participant
392 Views
5 Replies
Message 1 of 6

need help to modify the text replacement lisp file

kwankitfu
Participant
Participant

Below are the lisp file that i get from Open Ai. But its didn't work. My focus is to select text from drawing and open an excel xls file then search and matching the text in column A and then replace the text from the same row in column B.

 

(defun c:RPTXT (/ textObj excelFile excelData ss excelPath wb ws rng row col cell text)
; Get the selection set of text objects
(setq ss (ssget '((0 . "TEXT"))))

; Prompt the user to select an Excel file
(setq excelPath (getfiled "Select Excel File" "" "xls" 0))

; Open the selected Excel file
(setq excelFile (vlax-create-object "Excel.Application"))
(vlax-invoke-method excelFile 'Visible :vlax-false)
(setq wb (vlax-invoke-method excelFile 'Workbooks 'Open excelPath))
(setq ws (vlax-invoke-method wb 'Worksheets 'Item 1))

; Loop over the selected text objects
(vlax-for obj ss
(setq textObj (vlax-ename->vla-object obj))
(setq text (vla-get-textstring textObj))
(setq excelData (vlax-invoke-method ws 'UsedRange))
(vlax-for cell excelData
(setq row (vlax-get-property cell 'Row))
(setq col (vlax-get-property cell 'Column))
(when (= col 1)
(setq value (vlax-get-property cell 'Value))
(if (equal text value)
(vla-put-objectname textObj (vlax-get-property (vlax-invoke-method excelData 'Item row 2) 'Value))
)
)
)
)

; Save and close the Excel file
(vlax-invoke-method wb 'Save)
(vlax-invoke-method wb 'Close :vlax-false)
(vlax-release-object ws)
(vlax-release-object wb)
(vlax-release-object excelFile)

; Refresh the AutoCAD drawing to show changes
(command "REGEN")
)

0 Likes
393 Views
5 Replies
Replies (5)
Message 2 of 6

Kent1Cooper
Consultant
Consultant

@kwankitfu wrote:

... its didn't work. ....

 

....
(setq ss (ssget '((0 . "TEXT"))))

....

(vlax-for obj ss ....


"Didn't work" is never enough information.  Did it not load?  Did it load but not recognize the command name?  Did it recognize the command name but do nothing?  Did it do something, but not everything?  How far did it get?  Were there any error messages?  Etc., etc., etc.

 

In any case, I think at least one problem is the combination of the lines quoted above.  The (vlax-for) function takes a "VLA object representing a collection object," not an ordinary selection set such as will be in your ss variable.

Kent Cooper, AIA
0 Likes
Message 3 of 6

kwankitfu
Participant
Participant

its can load, can run, can select text, can prompt user to select excel file. But after excel file selected, it ended with Error: Automation error - "VISIBLE". i forgot to mentioned that i need to do multiple text replacement.

 

kwankitfu_1-1677122900167.jpegkwankitfu_2-1677122933369.jpeg

 

kwankitfu_3-1677122995711.jpeg

 

 

0 Likes
Message 4 of 6

kwankitfu
Participant
Participant

and the selected text didn't change at all

0 Likes
Message 5 of 6

ВeekeeCZ
Consultant
Consultant

At this point, your goal is too complex for the AI. And trying to fix its code is wasted time.

I would suggest to use THIS routine from Lee Mac.

0 Likes
Message 6 of 6

paullimapa
Mentor
Mentor

looks like it's erroring out here:

(vlax-invoke-method excelFile 'Visible :vlax-false)

not sure if this would make a difference but try:

(vla-put-visible excelFile :vlax-false)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes