Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel .AddPicture to inactive worksheet

Posted on 2014-11-14
4
Medium Priority
?
530 Views
Last Modified: 2014-11-14
To add picture 'myPic' (and onAction property) to Active worksheet("Item_Files"): I write this:

    Set wsIF = ThisWorkbook.Worksheets("Item_Files")
    With Range(Cells(3, 1), Cells(3, 1))
        ActiveSheet.Shapes.AddPicture "D:\path\myPic.jpg", msoCTrue, msoTrue, .Left, .Top, -1, -1
        ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Height = .Height
        ActiveSheet.Shapes(ActiveSheet.Shapes.Count).OnAction = "item_Files_shapeButtons"
    End With

- But, several pics are to be added at same time, and worksheet("Item_Files") is INACTIVE
------------

After a struggle, I found the code below added picture 'myPic' (and onAction property) to INactive worksheet("Item_Files"):
   
    Set wsIF = ThisWorkbook.Worksheets("Item_Files")
    With wsIF.Range("X1")
        wsIF.Shapes.AddPicture "D:\path\myPic.jpg", msoCTrue, msoTrue, .Left + 1, .Top + 1, -1, -1    
        wsIF.Shapes(wsIF.Shapes.Count).Height = .Height
        wsIF.Shapes(wsIF.Shapes.Count).OnAction = "item_Files_shapeButtons"
    End With

- One or more pics were delivered to the right sheet and cell, but to display the pics I had to Activate wsIF (ie  worksheet("Item_Files")), by clicking its tab.

Since clicking the tab is essential to see the result, I dont object to that (I only mention it in case you see signs of a flaw in my code)
But this is my first experience of adding pictures, and my next task is the add and remove some dynamically.

Each time I add a pic, I need to add  wsIF.Shapes(wsIF.Shapes.Count).NAME to a lookup list on another sheet.
So I need to repeatedly alternate between sheets.

THEREFORE I ask: is my code optimal - and what should I take on board for this exercise?

Many thanks
Kelvin4
0
Comment
Question by:Kelvin4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Assisted Solution

by:gowflow
gowflow earned 600 total points
ID: 40442396
As I see your code is fine and actually you thought me how to add a picture which I never did so far in VBA !!! :)
Now if you need more help to loop or anything pls let me know and much easier if you post a sample workbook we can then take it from there.

gowflow
0
 
LVL 31

Expert Comment

by:gowflow
ID: 40442428
BTW what is the macro item_Files_shapeButtons consist of could you post the code ?

And what do you mean by:

 added picture 'myPic' (and onAction property) to INactive worksheet("Item_Files"):

gowflow
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1400 total points
ID: 40442437
I would suggest that since Addpicture returns a reference to the shape added, you use it rather than relying on shapes.count:

Dim shp as Shape
 Set wsIF = ThisWorkbook.Worksheets("Item_Files")
    With wsIF.Range("X1")
        Set shp = wsIF.Shapes.AddPicture("D:\path\myPic.jpg", msoCTrue, msoTrue, .Left + 1, .Top + 1, -1, -1)
        shp.Height = .Height
        shp.OnAction = "item_Files_shapeButtons"
    End With

Open in new window


Note: you do not need to switch back and forth between sheets (in the sense of selecting/activating them) in order to add the names to a list or to add the shapes.
0
 

Author Closing Comment

by:Kelvin4
ID: 40443151
Hi goflow and Rory,
Your rapid comments were much appreciated, and you were right, not much needed to be changed.
But the point was I NEEDED some encouragement, and CHECKING that I was not going off crazily. And you offered it..
Hence points to both.

Set sh = wsIF.Shapes.AddPicture("D:Path|myPic.jpg"...    worked well

The only oddity was with the first code line (below). In my trials that line always read: With wsIF.range(Cells(3, i ) , Cells(3, i)) . But I built into the loop that drops successive graphics, another layer of conditions to control if a graphic was to be dropped (using conditions from data on a different ws). With that extra complication, the range statement wsIF.range(Cells(3, i ) , Cells(3, i))  blew up.Specifically: it would no longer work on an inactive sheet.

So! I guessed on: With wsIF.Cells(3, i ) and it worked on active and inactive sheets, though is that really a RANGE statement?
Maybe the With statement does not need to be a RANGE statement?!

                With wsIF.Cells(3, i + 1)
                    Debug.Print "Hi cells_cl = "; i + 1
                    Set sh = wsIF.Shapes.AddPicture("D:\Path\"myPic.jpg", msoCTrue, msoTrue, .Left, .Top + 1, -1, -1)
                    wsIF.Shapes(wsIF.Shapes.Count).Height = .Height
                    wsIF.Shapes(wsIF.Shapes.Count).OnAction = "Item_Files_Icons_onAction"
                End With

Its difficult to pass over a live file, as there is a graphics folder, and other external stuff.  But I'll return more details if its of any help in return. Now the piece works dynamically: individual graphics can be switched in and out of the sheet depending on conditions.

Kelvin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question