Jump to content
Sign in to follow this  
Jfish

Insert Image Into Excel

Recommended Posts

Jfish

Howdy all.  I was searching the net trying to find an easy way to insert an image into Excel when I came accross this VBA Code: http://www.exceltip.com/general-topics-in-vba/insert-pictures-using-vba-in-microsoft-excel.html.  I took a few minutes to translate it to AU3 and was pleased to see it actually worked.  I thought I would share it in case anyone needed something like it.

#include <Excel.au3>
#include <Array.au3>; for expansion to do many pics from an array

$oExcel = _ExcelBookNew()
$path = @ScriptDir&"\[YOUR IMAGE NAME]"
$targetRange=$oExcel.ActiveSheet.Range("B5:D10")

Func _TestInsertPictureInRange()
    _InsertPictureInRange($path, $targetRange)
EndFunc
_TestInsertPictureInRange()


func _InsertPictureInRange($path, $targetRange)
; inserts a picture and resizes it to fit the TargetCells range
Dim $p, $t, $l, $w, $h
    ; import picture
    $p = $oExcel.ActiveSheet.Pictures.Insert($path)
    ; determine positions
    With $targetRange
        $t = .Top
        $l = .Left
        $w = .Offset(0, .Columns.Count).Left - .Left
        $h = .Offset(.Rows.Count, 0).Top - .Top
    EndWith
    ; position picture
    With $p
        .Top = $t
        .Left = $l
        .Width = $w
        .Height = $h
    EndWith
    $p = "Nothing"
EndFunc

Enjoy.

  • Like 1

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
water

Thanks for the example. The rewrite of the Excel UDF contains such a function as well. It has some added functions to exactly place the picture and maintain the ratio if desired.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×