Jump to content

Insert Image Into Excel


Jfish
 Share

Recommended Posts

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.

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

Link to comment
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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...