Having problem identifying the function for this code any tips to stop error.

Having problem identifying the function for this code any tips to stop error.

thomas_huntFMPXR
Contributor Contributor
1,105 Views
36 Replies
Message 1 of 37

Having problem identifying the function for this code any tips to stop error.

thomas_huntFMPXR
Contributor
Contributor
(defun c:2 ( / file dlg datalinkName datalinkDict datalinkObj insPt range )
  (vl-load-com)
 
  ;;select Excel file
  (setq file (getfiled "Select Excel File" "" "xlsx" 0))
 
  (if file
    (progn
     
      ;; Get Datalinks dictionary
      (setq datalinkDict (vla-GetDataLinks (vla-Get-ActiveDocument (vlax-get-acad-object))))

      ;; Create new datalink
      (setq datalinkObj (vla-Add datalinkDict datalinkName file))

      ;; Optional: prompt for range
      (setq range (getstring T "\nEnter Excel range (e.g., Sheet1!A1:D10) or press Enter for full sheet: "))

      ;; Set the data range if provided
      (if (> (strlen range) 0)
        (vla-SetDataLinkRange datalinkObj range)
      )

      ;; Prompt for insertion point
      (setq insPt (getpoint "\nSpecify insertion point for the table: "))

      ;; Insert table from datalink
      (command "_-TABLE"
               "DATALINK"
               datalinkName
               (if (> (strlen range) 0) "Y" "N")
               (if (> (strlen range) 0) range "")
               insPt
      )
    )
    (prompt "\nNo file selected.")
  )
  (princ)
)
0 Likes
Accepted solutions (1)
1,106 Views
36 Replies
Replies (36)
Message 2 of 37

paullimapa
Mentor
Mentor

where you got this code from should include information on what it does.

also did you read the comments in the code which explains what each step does?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 37

Kent1Cooper
Consultant
Consultant

And what is the error mentioned in the Topic heading, that you want to stop?

Kent Cooper, AIA
0 Likes
Message 4 of 37

thomas_huntFMPXR
Contributor
Contributor

Function not identified after the selecting the excel file.

0 Likes
Message 5 of 37

thomas_huntFMPXR
Contributor
Contributor

Yes this portion of the code doesn't work the way it says it should "(setq datalinkDict (vla-GetDataLinks (vla-Get-ActiveDocument (vlax-get-acad-object))))."

0 Likes
Message 6 of 37

cadffm
Consultant
Consultant

Hi,

 

why should vla-GetDataLinks  and vla-SetDataLinkRange work?

[F1] Document

 

 
 

 

 

Sebastian

0 Likes
Message 7 of 37

thomas_huntFMPXR
Contributor
Contributor

it needs to activate datalink command to connect the path to taband range in order to complete the datalink and insert the table.

0 Likes
Message 8 of 37

cadffm
Consultant
Consultant

I can't follow you.

 

From where is that code?

If it is from AI, ask your AI what's going on!?

Sebastian

0 Likes
Message 9 of 37

thomas_huntFMPXR
Contributor
Contributor

AI has no solution. I would like the datalink to connect to new file and then create a table based on the datalink that was just established.

0 Likes
Message 10 of 37

cadffm
Consultant
Consultant

@cadffm

 

From where is that code?

 

Sebastian

0 Likes
Message 11 of 37

Moshe-A
Mentor
Mentor

@thomas_huntFMPXR ,

 

;; Get Datalinks dictionary
      (setq datalinkDict (vla-GetDataLinks (vla-Get-ActiveDocument (vlax-get-acad-object))))
 
This call is not valid in autolisp activex so from here your error in coming.
on the other hand using (command "datalink") also won't help because DATALINK has no command line interface.
 
Moshe
 
0 Likes
Message 12 of 37

thomas_huntFMPXR
Contributor
Contributor

Alright, is there a lisp code that creates a table from the datalink data? All the codes I found create a empty table with no information.

0 Likes
Message 13 of 37

Moshe-A
Mentor
Mentor

@thomas_huntFMPXR ,

 

if you have excel file linked into AutoCAD with DATALINK, you can insert it using TABLE command. can you explain why you need lisp to to that?

 

Moshe

 

0 Likes
Message 14 of 37

thomas_huntFMPXR
Contributor
Contributor

Wanted a to create a shortcut to limit confusion when inserting new tables. my company operates with 10+ datalink tables at a time.

 

0 Likes
Message 15 of 37

Sea-Haven
Mentor
Mentor

Not tested but may be useful.

 

 

 

0 Likes
Message 16 of 37

thomas_huntFMPXR
Contributor
Contributor

Tested it out. had to change the defun command and change the file selection for excel. but this syntax error and server returned error.

 

0 Likes
Message 17 of 37

Sea-Haven
Mentor
Mentor

I did find a typo in the code as I suggested I did not test, but I have now table is being made but link is not being established, will look into it more why not working.

Message 18 of 37

thomas_huntFMPXR
Contributor
Contributor

yeah thank I will tinker with it some more as well.

 

0 Likes
Message 19 of 37

Moshe-A
Mentor
Mentor

@thomas_huntFMPXR hi,

 

Here you go with TableFromDataLink (insert table from assign datalink)

 

I notice a problem (on and off) with inserting a table using TABLE command (not the lisp)

anyway if it works, you will see a big delay, this is OK, it takes time to AutoCAD to insert excel table.

 

enjoy

Moshe

 

0 Likes
Message 20 of 37

Sea-Haven
Mentor
Mentor

Thanks @Moshe-A gave it a try, but I think the idea is to not use dialog boxes but rather know the Excel file name and maybe the desired range. Just a ps did not work on my Bricscad V25. Just opens a new Blank Excel.

 

Will go back to other code has a cons error, problem is so many of them.

0 Likes