Jump to content

HELP: open error excel


MUTHUKUMAR1983

Recommended Posts

(defun c:ExportAngleToExcel (/ excel-app excel-wb excel-ws angle p1 p2 row-num)
  ;; Set the existing Excel file path
  (setq file-path "C:\Path\To\Your\File.xlsx")

  ;; Create an instance of Excel application
  (setq excel-app (vl-catch-all-apply 'vlax-get-or-create-object '("Excel.Application")))

  ;; Open the existing workbook
  (setq excel-wb (vl-catch-all-apply 'vlax-invoke-method excel-app 'Workbooks 'Open file-path))
  (setq excel-ws (vl-catch-all-apply 'vlax-get-property excel-wb 'ActiveSheet))

  ;; Find the last used row in the Excel sheet
  (setq row-num (+ (vl-catch-all-apply 'vlax-get-property excel-ws 'UsedRange.Row)
                   (vl-catch-all-apply 'vlax-get-property excel-ws 'UsedRange.Rows)
                   1))

  ;; Loop to get multiple angles
  (repeat 5 ; Change the number according to your requirement
    ;; Prompt the user to select the first point
    (setq p1 (getpoint "\nSelect first point: "))

    ;; Prompt the user to select the second point
    (setq p2 (getpoint p1 "\nSelect second point: "))

    ;; Calculate the angle between the two points
    (setq angle (rtos (angtos (angle p1 p2)) 2))

    ;; Write the angle value to Excel
    (vl-catch-all-apply 'vlax-put-property (vl-catch-all-apply 'vlax-get-property excel-ws 'Cells row-num 1) 'Value (strcat "Angle " (itoa (1+ row-num))))
    (vl-catch-all-apply 'vlax-put-property (vl-catch-all-apply 'vlax-get-property excel-ws 'Cells row-num 2) 'Value angle)

    ;; Increment the row number
    (setq row-num (1+ row-num))
  )

  ;; Activate Excel application to make it visible
  (vl-catch-all-apply 'vlax-invoke-method excel-app 'Activate)

  ;; Save and close the Excel workbook
  (vl-catch-all-apply 'vlax-invoke-method excel-wb 'Save)

  ;; Close the Excel workbook without saving changes
  (vl-catch-all-apply 'vlax-invoke-method excel-wb 'Close :vlax-false)

  ;; Quit Excel application
  (vl-catch-all-apply 'vlax-invoke-method excel-app 'Quit)

  ;; Release COM objects
  (vlax-release-object excel-ws)
  (vlax-release-object excel-wb)
  (vlax-release-object excel-app)

  ;; Clear any unreferenced symbols
  (gc)
  (princ)
)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...