+ Reply to Thread
Results 1 to 13 of 13

insert image

  1. #1
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    insert image

    Hi there, I'm new to the forum and to VBA.
    I have a multi-row ,multi-column spreadsheet which lists furniture items, and with some plain Excel formulas allows me to create a proper list.
    I'm using Excel as a database, in Sheet1 I have all the data, and Sheet2 is actually where I select the data and set the list of the chosen items.
    Now, I have the images for all the items listed in Sheet1 in the same folder of the xlsm file, and I want to insert the proper image in a given cell in the given row.
    Looking for something which may do the trick I did find this code in your site and it actually works for me!
    Now, what I need is to provide the PictureFileName and TargetCell via parameters already present in the spreadsheet.
    Say I want to retrieve the image shelves.jpg which is in the folder DATABASE\imgs\ in your code mentioned above I manually set the string for PictureFileName like this
    Please Login or Register  to view this content.
    and say I want to put the image in D10 I indicate
    Please Login or Register  to view this content.
    .

    Well, I did find another loop code on the net which sets my row value to 1 and increase it by 1 after each insert picture until the cell on the 2nd column is null.
    Now say that I have in each row, on column K the image name, and on column L the path I tried to retrieve the whole path like this:
    Please Login or Register  to view this content.
    The whole code I set up is this below
    Please Login or Register  to view this content.
    I guess the problem is that the full image path string is not correct, because I tried it in the Immediate and it gives me null, while manually works fine.
    Any help would be greatly appreciated.

    P
    Last edited by fredpox; 02-27-2010 at 11:06 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image - Excel 2007 VBA

    Maybe something like this,

    Please Login or Register  to view this content.
    If you still have problems then post a small example file with a few records.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image - Excel 2007 VBA

    Hi Andy, your code works like a charm! thanks a lot!
    Now I'm trying to create an "opposite" code which deletes all the images inserted in the document.
    Here I did find this code
    Please Login or Register  to view this content.
    and I'm thinking about how to change it to adapt to my needs.
    In this example the user wanted to retrieve all the images whose names were starting by Picture, but I don't have such standard names.
    I tried to change the sub like this
    Please Login or Register  to view this content.
    By simply deleting the If statement the sub would delete all the images in my active worksheet which is not what I want.

    Thanks again in advance for your precious help!

    P.

    PS: next and final step would be creating a warning when the user clicks the button for inserting the images: if the images are already inserted in the document the sub doesn't have to re-insert them again. Am I supposed to create another thread for that? thanks

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image

    It would be better if you named the shapes that you insert.

    Please Login or Register  to view this content.
    Now you can use the cell information to delete or check for existing shape
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image

    Hi Andy and thanks again for your support
    As soon as I go on on this project I realize how my knowledge of excel and vba is poor...
    anyway, I'm here to learn, so I'll try to become a good student
    I implemented your code in my file but I still have troubles: when I load the images (Insert Images button) everything works fine, but when I press the other button (Delete images) I get a 400 error. Online debug tells me it's a 1004 runtime error.

    I attach my xlsm file again since I inserted a few records, formulas and images and maybe it's easier to understand what I'm apparently missing..

    I changed the positioning of the *significant-values* columns but I also changed the code, in fact the images load correctly: on column 6 (F) I have all my pictures names which I get with the codes you corrected.

    Thanks again for your time, my turn next time

    P.
    Attached Files Attached Files

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image

    Firstly the names of the images does not match the contents of column F.
    If you rename the images then this modified code will remove them.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image

    Hi Andy
    I'm having hard times in figuring out what do you mean by saying
    Firstly the names of the images does not match the contents of column F.
    If you rename the images then this modified code will remove them.
    .
    Maybe my poor understanding of shapes in Excel VBA doesn't allow me to get the gist of your comment.
    The images loads with the first function you corrected because they do match the name in column F plus a .jpg.
    If I check the alt_text of my images still tells me the correct image name I inserted (ie scaffalature.jpg).
    How do I rename my images? You mean in my Excel file or my actual jpg files? When you say shapes you are talking about shapes created in Excel and images as well? are all of them the same for Excel?

    Thanks for your patience....
    P

    PS: I implemented your last code and now it deletes me just the 2 command buttons, the images are still in place..

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image

    What I mean is if you select the image that is in row 3 with F3: =scaffalature

    The name of the image is not "scaffalature.jpg" but "Immagine 33". This would suggest that either the image was added manually or that the code did not name the shape when inserting it.

    Once you name the images correctly the delete code will work.

    The reason the buttons are deleted is that they are named
    divano.jpg and cuscino.jpg

  9. #9
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image

    Hi Andy!
    Much clearer now, thanks for the explanation!
    I did see the images names are not ie. scaffalature.jpg but immagine13.
    Now I get it, the code won't work until I rename each of them in the Excel name box, in fact by renaming them and the two buttons the delete code works fine.
    Now the only question is: since I imported the images via code, and not manually, is there a way to "rename" the images in Excel via code after importing them or have I to rename each of them manually?

    I thought your code
    Please Login or Register  to view this content.
    would rename the images as soon as they're loaded in Excel but apparently it doesn't work as expected..

    ......
    I did it!
    I changed your code like this
    Please Login or Register  to view this content.
    I used the object Pictures instead of Shapes and it worked!


    Last question on this topic, I promise: I would like to prevent the user to load once again the images if they're already present in the document. Is there a way to do so?
    I want to exit the TestInsertPicture Sub if the images are loaded, and show a message, otherwise the sub will take place and will insert the pictures

    I thought of some if statement to run before the while loop, something like this, but *obviously* it doesn't work
    Please Login or Register  to view this content.
    I get an error on IsNull() : non optional argument. How can I tell Excel to handle null values in this example?

    Thanks again..
    P

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image

    You can use the FindByName function again. This time if nothing is found you can insert the picture.

    Double check column numbers are correct.
    Also this is explicitly for Row 2.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image

    Hi Andy and sorry for the delay of my answer, I hadn't had the chance to reply to you sooner.
    I changed the code as per your suggestion but I get a 400 error.
    If I keep my previous code with while loop Excel crashes when I try to re-insert the images..
    I'm sorry to bother with this again but this is the very last one step for getting the job done.

    Thanks again for your time!
    P

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: insert image

    These revised routines will insert and delete images. If the image is already inserted it will not be inserted again.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: insert image

    Thank you very very much Andy!!
    Hope to have the chance to return you the favour sometimes.

    Have a nice day!
    P

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1