Sign in to follow this  
Followers 0
Jfish

Insert Image Into Excel

3 posts in this topic

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.

1 person likes this

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



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.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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  
Followers 0