+ Reply to Thread
Results 1 to 33 of 33

Form: Load Picture from Worksheet instead of a folder structure

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Form: Load Picture from Worksheet instead of a folder structure

    Hello All,

    I have done extensive searching all over the internet for what I thought would be a simple answer, but this one is proving to elude me.

    This is to load a picture onto an image in a form.

    How do I change this line here...(which works)

    Please Login or Register  to view this content.


    to instead, say this here...

    Please Login or Register  to view this content.



    I need to load pictures from my macros startup workbook rather than a folder structure because otherwise, this code won't work on another computer.


    UPDATE: I tried this and it didn't work..

    Please Login or Register  to view this content.
    Thanks much
    Last edited by duugg; 11-18-2011 at 09:05 PM.

  2. #2
    Registered User
    Join Date
    07-31-2010
    Location
    essex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Hi

    you could upload your pictures to a photo hosting website such a photobucket.com and then use a directlink to load your picture.

  3. #3
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Hmm,

    Thanks lima, but I'm really looking for something like in my first post. There will be times when I won't have an internet connection as well.

    Wow, it really seems like this is not possible to do with VBA. It seems like such a simple thing...to load an image into a form from a worksheet, rather than from a folder structure.


    Any more thoughts from anyone?


    Thanks

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Hello duugg,

    Things are not always what they seem as is the case here. The Image control expects the picture to be in a format it understands. The LoadPicture method converts picture files stored on disk into the picture format the Image control understands.

    The problem with loading a picture from a workbook is the picture is stored within the workbook, not as a separate file on the disk. To load a picture from a workbook into a Image control requires copying the picture to clipboard and then converting the picture on the clipboard into the picture format that the Image control understands.

    Your profile says your are using Excel 2003. If you plan to use this on other platform like 2007 or 2010 then let me know. I have code to do this but made need some modification if it is used on later versions of Windows.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Smile Re: Form: Load Picture from Worksheet instead of a folder structure

    Hello Leith!

    Thanks for responding. You've helped me out with other dilemnas in the past and I am totally excited for any code you want to throw my way .

    If anyone can solve this problem, it's you!

    I also have great news. I forgot to update my Excel version. I am now using Excel 2007!


    I hope that helps make it easier?


    Thanks much

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Form: Load Picture from Worksheet instead of a folder structure

    A real 'workaround':

    Please Login or Register  to view this content.



  7. #7
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Question Re: Form: Load Picture from Worksheet instead of a folder structure

    snb,

    Thanks much for the reply. I tried your code out and got this error here...


    "Run-time error '-214xxxxxx (70880057)':

    The index into the specified collection is out of bounds.
    Note that I made two changes to the code...

    Change 1 - I changed "\foto.gif" to "\picture 1.jpg" as that is the name of my picture.
    Change 2 - I changed "GIF" to "JPG".


    In regards to this line of code here...

    Please Login or Register  to view this content.

    Does that mean that it looks for my picture on "Sheet1"? If not, how can I point your code to the proper sheet?


    Thanks much!

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Use F8 in the VBEditor to specify exactly in which line the error occurs..
    The file should be saved once otherwise thisworkbook.path errors out.

    Please Login or Register  to view this content.
    Last edited by snb; 11-16-2011 at 11:47 AM.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    If you add the picture to the sheet using an ActiveX Image control, you can simply assign the picture property of that control to the picture property of the image control on the form.
    Everyone who confuses correlation and causation ends up dead.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    PS Alternatively, with a standard picture, you can use Stephen Bullen's PastePicture code:
    Please Login or Register  to view this content.
    and then just use in the userform:
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Wow, I might be getting over my head on this one fellas.

    snb -

    It bugs on
    Please Login or Register  to view this content.
    Also, I'm not sure that this matters, but I run this macro FROM my startup book but the macro is executed on a workbook (though this has been saved once prior to the macro).
    Using your code snb, how does the code know which picture to grab when there is more than one?

    My pictures are named "Picture 1" and "Picture2" on the worksheet named "sheet1".



    romperstomper -

    I only want to import file extensions that Powerpoint will save. I believe one compatible extension is bmp. I can save all my powerpoint slides as bmp.

    Now here's my questions for you romerstomper...

    Thank you thank you for the code, but it is wayyy to complicated for a newbie like me to understand without some additional assistance..

    Question 1. Let's assume I will ONLY put in bitmaps, is there a way that you could delete any code that references any picture types OTHER than "bmp"? That should shorten the code a bit and maybe I could understand it better.

    Question 2. Let's assume the following...
    a) I'm running this code from my startup macro workbook named "Macros"
    b) I'm executing it on a workbook named "DB" and a worksheet named "sheet1"
    c) I have only 2 pictures named "Picture1" and "Picture2"

    Question 3. Having said the above, would you either modify the code to reflect the conditions above or highlight any code that I need to modify for my unique situation above in RED?

    Thanks very much to the both of you for helping me out with this one. This is a year-long macro I've been working on and this is my second to last step.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    I don't see what PowerPoint has to do with anything. The code does not care what format picture you inserted on the sheet.

    Where exactly are you running the code from? A userform in the personal macro workbook? If so, and assuming 2 image controls, the code in the form will be:
    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    romperstomper,

    Ok, there's what I did so far...

    I tried this code here...

    Please Login or Register  to view this content.


    and I got this error here...

    Compile error:

    Sub or Function not defined
    My form name is "UF_Status". Would that affect this error maybe?


    UPDATE:

    Am I still supposed to paste in Stephen Bullen's Paste Picture code as well? If so, I tried that and got an error saying this...

    Compile error:
    "Only comments may appear after End Sub, End Function of End Property"
    Any thoughts?



    Also, am I supposed to put some code in the UF_Status form? It currently looks like this...
    Please Login or Register  to view this content.


    Thanks
    Last edited by duugg; 11-16-2011 at 03:57 PM. Reason: Clarity, I hope

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Did you add the first bit of code I posted to a new module in the same workbook as the code you just posted?

  15. #15
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Yes, I pasted in Stephen Bullen's Paste Picture code and got an error saying this...


    Compile error:
    "Only comments may appear after End Sub, End Function of End Property"

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Then I guess you did not add it to a new module?

  17. #17
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Ahhhhh,

    So the first part of that code has to be in its own module, separate from the second part of the code?

    I didn't realize that.


    Ok, I put each code section in it's own module.

    I tried to put a "Sub" and "End Sub" for the long code, but realized that neither should be in there (I think anyway) so I took them both out. When I did that I got this error message on the long code when trying to run the short code from a different module...

    "Compile Error:

    "Invalid outside procedure"

    Also, I noted the text in the code below...
    ''' Requires a reference to the "OLE Automation" type library"
    How can I confirm if I have that reference or not?

    Thanks again for your help.

  18. #18
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Form: Load Picture from Worksheet instead of a folder structure

    To illustrate my code suggestion, cfr. the attachment
    Attached Files Attached Files
    Last edited by snb; 11-16-2011 at 05:03 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    snb,

    Thanks for the reply. I will look into this later on tonight.

    Arggg, the last steps of any big project always seem to be the hardest!

    Thanks

  20. #20
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Not sure what you mean by "each code section"? In my original 'PS' post there are two code blocks. The first one is a complete module. The second was an example of how to call the first, but that was replaced by the specific code for your workbook/sheet/picture combinations.

  21. #21
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Form: Load Picture from Worksheet instead of a folder structure

    I also implemented RS's suggestion to use Active X controls in the sheet. (by far the simplest method)
    Attached Files Attached Files

  22. #22
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Wow guys, I totally appreciate you sticking with me on this as long as you have been able to.

    It's hard for you guys to understand my confusion because you both obviously are very well versed with VBA and I am totally jealous! lol .

    I'm going to explain in laymens terms here my thoughts and dilemnas and maybe after that, you will be able to give this another attempt if you so wish to.

    Okay, so, I am working with 2 workbooks here.

    Workbook 1 - Named "Macros", located in my startup folder and is where all my macros are stored in about 80 modules or so.

    Workbook 2 - Named "DB Only", saved as an XLSM file so that I can run macros on it.



    In my Macro workbook...

    I have a form named "UF_Status", and within that form, I have "Image1" and "Image2" which are blank and have no pictures/images loaded into them.

    I have a worksheet named "sheet1" (decided it should be in my "Macros" workbook because I want to save with images with my macro workbook and NOT the "DB Only" workbook.

    On the worksheet named "sheet1", I have 2 images named "picture 1" and "picture 2".


    In my DB Only workbook...

    The only worksheet I have in this workbook is called "DB"


    Here's what I want...
    (keep in mind that I can already do this perfectly by loading the images from a folder location, which is really not what I want)...


    While on the DB Only workbook...

    1. I want to press a button on my toolbar assigned to run a macro

    2. I then want 2 things to take place
    a) I want to run this code below...

    (note that my code is obviously much much longer than what is below, I simply chose this code because it's short and simple...)
    Please Login or Register  to view this content.

    B) WHILE the above code is running..I would like "Picture 1" to show up and then go away, then have "Picture 2" show up and then go away.

    And that's it!


    Now, from what I gathering here, there seems to be 6 areas that can modified with either code, names or properties..

    Area 1. The "sheet1" worksheet of the "Macros" workbook
    Area 2. The "sheet1" code of the "Macros" workbook
    Area 3. The code area of the form named "UF_Status" in the "Macros" workbook which looks like this...

    Please Login or Register  to view this content.

    Area 4. The properties area of the form named "UF_Status" in the "Macros" workbook.
    Area 5. Module "X" - That "long" code that romperstomer provided" that begins with this..
    Please Login or Register  to view this content.

    Area 6. Module "Y" - That "short" code which is here..

    Please Login or Register  to view this content.

    So, having said that here are my questions...

    Question 1 - AREA 2 - Do I need to put anything at all in AREA 2, the code of the sheet1 WB?

    Question 2- AREA 3 - Code area of the form - Do I need to put anything in this area?

    Question 3 - AREA 4 - Properties area of the form - Do I need to change any values in this area, name, pre-load the images, etc?

    Question 4 - AREA 5 - Module "X" - Other than copying that long code, is there anything at all i need to modify? For example, what should the module be named? I assume that there's no "Sub" and "End Sub" anywhere in this module?

    Question 5 - AREA 6 - The "Short" code - Again, other than copying the short code to a module, is there anything I need to modify? What should this module be named?


    Having stated the above, I just realized that there should be NO code at all in the "DB Only" workbook. I want all the pictures and code to reside in the "Macros" workbook so that this awesome macro will work on any workbook, using any person's computer (providing I put the "Macros" workbook in their startup of course.)


    I'm basically looking for this at this point...

    1. Put THIS code in "AREA 1"
    2. Put THIS code in "AREA 2"
    3. Name THIS module "X"
    etc, etc, etc.


    Wow, I am sorry to be so technical with my questions here guys. FYI - I have given you both as many reputation points as possible I have learned sooo much from this forum in the past year or so. Forms and even functions to a degree are brand new to me and this is proving to be a real challenge. Do whatever you can and even if you can't do anymore, thanks so much for getting me to this point!
    Last edited by duugg; 11-17-2011 at 12:19 AM.

  23. #23
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    You say you already have this working from folders so:
    Area5 No change.
    Area6 - this code basically replaces the code you are using to load the pictures from a folder. It is not a complete sub - it is just a couple of code lines that you put in your existing sub.
    That's it.

  24. #24
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Here is a demo file in case it makes things clearer.
    Attached Files Attached Files

  25. #25
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Ok I think we're getting somewhere now.

    I don't want to have to click on anything INSIDE any worksheet.


    I want to ONLY run a macro from my "Macros" workbook. I don't want any "click here" buttons on any worksheet.


    So, let's say I have this working code here...

    Please Login or Register  to view this content.
    WHILE that code is running on my "DB" worksheet, I want to display a user form "mini-window" (form) that automatically, executes the loading and unloading of "Picture 1" and "Picture 2", respectively.

    So, when a user starts a macro, they will see...

    1. The worksheet scrolling up and down (useless code I know, I will replace with my own code).

    2. A mini-window (or form) displaying pictures while the scrolling up and down is still visible behind this window.

    3. When the last bit of code is executed, the mini-window (or user-form) simply disappears.


    This works perfectly now except the images are loaded via a folder structure.

    My form is named "UF_Status".


    I hope that helps.

    Thanks

  26. #26
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    I'm still not seeing what the disconnect is. You say (but still haven't shown) that you have code to load the pictures from a file. We have given you samples showing how to load the picture from a picture on a sheet. All you need to do is replace one with the other. It does not have to run from clicking anything - that is simply a demonstration.

  27. #27
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Ahh, yes, I suppose that would help to see all the code.

    Sorry about that, here it is...

    Please Login or Register  to view this content.

    Note that the code refers to the "UF_Status" form.

    Hopefully seeing all the code (something I should have done before) will make it much easier to convey.

    Thanks

  28. #28
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    Please Login or Register  to view this content.

  29. #29
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Thumbs up Re: Form: Load Picture from Worksheet instead of a folder structure

    HEY NOW!

    HOW DO YOU SPELL SUCCESS!!!



    WWwwwooowwwWW



    Unbelievable!

    My only regret is not providing the whole code earlier!


    Thank you thank you thank you!

    Puuurrrfection!!!!!

    P.S. I tried to mark the thread "solved" but was unable to.
    Last edited by duugg; 11-17-2011 at 01:05 PM.

  30. #30
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Form: Load Picture from Worksheet instead of a folder structure

    In the meanwhile I was working on a 'switching' variant.
    cfr. the attachment
    Attached Files Attached Files
    Last edited by snb; 11-17-2011 at 01:45 PM.

  31. #31
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Form: Load Picture from Worksheet instead of a folder structure

    snb,

    Thanks for the effort. It is still much appreciated. I'd be curious with your and romperstomper's thoughts on how romper's last code nailed it without the "Stephen Bullen" module he provided in his first post.

    Who would've thought that this could be accomplished with such little code? I had initially thought it should've been pretty short code from the beginning but didn't know the syntax to use. Of course, again, my bad for not providing the entire code from the beginning.

    I guess I'm asking if the successful short code is doing everything that the "Stephen Bullen" code/module is but in a much cleaner and shorter way?

    What are you guys thoughts on this?

  32. #32
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Form: Load Picture from Worksheet instead of a folder structure

    That code only works if you also have Stephen's code too.

  33. #33
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Thumbs up Re: Form: Load Picture from Worksheet instead of a folder structure

    Ohhh, lol, I didn't even think of trying your code while have that module removed. Thanks for letting me know and thanks again.

+ 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