Insert a picture to an Excel file using Autolisp

Insert a picture to an Excel file using Autolisp

Kh.mbkh
Advocate Advocate
587 Views
6 Replies
Message 1 of 7

Insert a picture to an Excel file using Autolisp

Kh.mbkh
Advocate
Advocate

Want to insert a picture to Excel file using Autolisp:

 

(setq Path (getvar "dwgprefix"))
  (setq ExFile (strcat Path "TestP.xlsx"))
  (if ExFile
    (progn
      (setq excel-app  (vlax-get-or-create-object "Excel.Application"))
      (vla-put-visible excel-app :vlax-false)
      (vlax-put-property excel-app "ScreenUpdating" :vlax-false )
      (setq wbook-coll (vlax-get-property excel-app "Workbooks"))
      (setq wbook  (vlax-invoke-method wbook-coll 'Open ExFile))
      (if (or (null wbook) (vl-catch-all-error-p wbook))
	(progn
	  (alert "Can not find ExcelFile ")
	  (vlax-put-property excel-app "ScreenUpdating" :vlax-true )
	  (vlax-invoke-method wbook-coll "close"  ) ; close
	  (vlax-invoke-method excel-app "quit"  )   ; Quit
	  )
	(progn
      (setq sheet-coll (vlax-get-property wbook "Sheets"))
      (setq TruesheetDist (vlax-get-property sheet-coll "item" 1))
      (setq Cells (vlax-get TruesheetDist "cells"))
; Here want to insert a picture (knowing its path and name) in the "TruesheetDist"
0 Likes
Accepted solutions (1)
588 Views
6 Replies
Replies (6)
Message 2 of 7

Sea-Haven
Mentor
Mentor

Ok start with this its a excel macro.

Sub Macro1()
Dim strFile As String

strFile = Application.GetOpenFilename(FileFilter:="Excel files (*.png*), *.jpg*", Title:="Choose an image file to open", MultiSelect:=False)
 
ActiveSheet.Pictures.Insert(strFile).Select
 
End Sub

 

I have posted the question elsewhere need a VL version of ActiveSheet.Pictures.Insert(strFile).Select

 

When you insert an image its added to the Shapes collection. I dont have a problem with VL -> excel but struggling on this task and can not find anything. 

 

 A side note have worked out how to find size of an image in Excel as you may want to reset size. It has a top right cell with an ending cell and a width.

 

0 Likes
Message 3 of 7

MunteanStefan
Contributor
Contributor
Accepted solution

A picture is a shape object in the shapes collection of a sheet.

Use AddPicture or AddPicture2 method to insert a new picture

 

(setq Path (getvar "dwgprefix"))
  (setq ExFile (strcat Path "TestP.xlsx"))
  (if ExFile
    (progn
      (setq excel-app  (vlax-get-or-create-object "Excel.Application"))
      (vla-put-visible excel-app :vlax-false)
      (vlax-put-property excel-app "ScreenUpdating" :vlax-false )
      (setq wbook-coll (vlax-get-property excel-app "Workbooks"))
      (setq wbook  (vlax-invoke-method wbook-coll 'Open ExFile))
      (if (or (null wbook) (vl-catch-all-error-p wbook))
    (progn
	  (alert "Can not find ExcelFile ")
	  (vlax-put-property excel-app "ScreenUpdating" :vlax-true )
	  (vlax-invoke-method wbook-coll "close"  ) ; close
	  (vlax-invoke-method excel-app "quit"  )   ; Quit
	  )
    (progn
      (setq sheet-coll (vlax-get-property wbook "Sheets"))
      (setq TruesheetDist (vlax-get-property sheet-coll "item" 1))
      ;(setq Cells (vlax-get TruesheetDist "cells"))

      ;Insert picture
      (setq shapes (vlax-get-property TruesheetDist "Shapes"))
      (vlax-invoke shapes
        "AddPicture"               ;AddPicture2 - alternative method
        "D:\\Pictures\\Logo.jpg"   ;filename
        :vlax-false                ;link to file, True or False
        :vlax-true                 ;save with the document, True or False. Must be True if Link is False
        0                          ;x position, in pixels, from A1 top-left corner
        0                          ;y position, in pixels, from A1 top-left corner
        -1                         ;width, in pixels. -1 to retain the original size
        -1                         ;height in pixels. -1 to retain the original size
        )
      )
    )
  )
)

 

0 Likes
Message 4 of 7

Kh.mbkh
Advocate
Advocate
Thank you for your time, that's good!.
Is there a way to place the picture in a given cell (example "B5").
Thanks !
0 Likes
Message 5 of 7

Kh.mbkh
Advocate
Advocate
Thnak you, good! this is another way to do ...
0 Likes
Message 6 of 7

MunteanStefan
Contributor
Contributor

Use "Left" and "Top" properties of a cell to get the coordinates.

 

(setq Path (getvar "dwgprefix"))
  (setq ExFile (strcat Path "TestP.xlsx"))
  (if ExFile
    (progn
      (setq excel-app  (vlax-get-or-create-object "Excel.Application"))
      (vla-put-visible excel-app :vlax-false)
      (vlax-put-property excel-app "ScreenUpdating" :vlax-false )
      (setq wbook-coll (vlax-get-property excel-app "Workbooks"))
      (setq wbook  (vlax-invoke-method wbook-coll 'Open ExFile))
      (if (or (null wbook) (vl-catch-all-error-p wbook))
    (progn
	  (alert "Can not find ExcelFile ")
	  (vlax-put-property excel-app "ScreenUpdating" :vlax-true )
	  (vlax-invoke-method wbook-coll "close"  ) ; close
	  (vlax-invoke-method excel-app "quit"  )   ; Quit
	  )
    (progn
      (setq sheet-coll (vlax-get-property wbook "Sheets"))
      (setq TruesheetDist (vlax-get-property sheet-coll "item" 1))
      (setq Cells (vlax-get TruesheetDist "cells"))
      (setq cell (vlax-variant-value
                   (vlax-get-property cells "Item" 5 2);"B5" - row = 5; column = 2
                 )
      )
      (setq left (vlax-get cell "Left")
            top  (vlax-get cell "Top")
      )

      ;Insert picture
      (setq shapes (vlax-get-property TruesheetDist "Shapes"))
      (vlax-invoke shapes
        "AddPicture"               ;AddPicture2 - alternative method
        "D:\\Pictures\\Logo.jpg"   ;filename
        :vlax-false                ;link to file, True or False
        :vlax-true                 ;save with the document, True or False. Must be True if Link is False
        left                       ;x position, in pixels, from A1 top-left corner
        top                        ;y position, in pixels, from A1 top-left corner
        -1                         ;width, in pixels. -1 to retain the original size
        -1                         ;height in pixels. -1 to retain the original size
        )
      )
    )
  )
)

 

0 Likes
Message 7 of 7

Sea-Haven
Mentor
Mentor

Thank you the missing bit was I was using 2 words, when it should have been 1, Add+Picture, Addpicture.

 

 

0 Likes