PDA

View Full Version : Help: how to insert pic, textbox to chart automatically???



yurble_vn
06-24-2007, 03:24 AM
I have used this code to insert picture to all chart in the selected worksheets. It works at some first times. But after that, it can not run anymore. And some time, it ignore some chart.

I can not find out where is the bug... please help....


Sub Insert_Pic()
Dim icht As Integer

For icht = 1 To ActiveSheet.ChartObjects.Count
ActiveSheet.ChartObjects(icht).Activate
ActiveChart.ChartArea.Select
ActiveChart.Pictures.Insert("F:\My Documents\My Pictures\PI logo\cola.jpg"). _
Select
Next
End Sub


The error comes from :

ActiveChart.Pictures.Insert("F:\My Documents\My Pictures\PI logo\cola.jpg"). _
Select

mdmackillop
06-24-2007, 10:41 AM
I can't see a reason for the problem. Could it be memory?. How big are your pictures and how many charts.
BTW, your code can be simplified slightly
Sub Insert_Pic()
Dim icht As Integer

For icht = 1 To ActiveSheet.ChartObjects.Count
ActiveSheet.ChartObjects(icht).Activate
ActiveChart.Pictures.Insert ("F:\My Documents\My Pictures\PI logo\cola.jpg")
Next
End Sub

yurble_vn
06-25-2007, 08:57 AM
Thanks for prompt reply but not solved the problem, I have embeded the file for your checking.

BTW, the file is small, just a 7kb file.

yurble_vn
06-25-2007, 09:07 AM
Just to make the trouble clearer:

After testing, i found out that: this code just insert pic to charts that appear in the screen. When I zoom out so all charts show in screen, it insert to all charts.
How to fix the problem?

mdmackillop
06-25-2007, 03:10 PM
This is better, but I'm needing to use BringToFront on one symbol
Sub Insert_Pic()
Dim icht As Integer
Dim w As Window
Application.ScreenUpdating = False
Range("A1").Select
Set w = ActiveWindow
For icht = 1 To ActiveSheet.ChartObjects.Count
w.ScrollRow = ActiveSheet.ChartObjects(icht).TopLeftCell.Row
ActiveSheet.ChartObjects(icht).Activate
ActiveChart.Pictures.Insert ("C:\cola.jpg")
Next
w.ScrollRow = 1
Set w = Nothing
Application.ScreenUpdating = True
End Sub

yurble_vn
06-25-2007, 09:49 PM
Thank Mdmackillop, it works great.

But, after some test, I found out: It would be better if the follow bug did not occure (I really dont know where it come). I mean your code is really works great, it's just a small bug that I found accidentally:

Let me describe:

After the first run, I tried to select one pic in one chart. and keep it selected. Then I run the macro. and from that time on, the macro always stop in that chart. When I select the chart again after that, I note that, the new inserted and the elder picture are both selected, and I was unable to deselect all pic in that chart. Always have one pic selected.

Is there anyway to deselect all selection in chart before insert pics in it?

Once again, thanks for prompt support.

Sincerely

BTW, I add some code to run macro all selected sheet. I'm just intending to insert logo into all chart.

Sub Insert_Pic()
Dim icht As Integer
Dim wks As Worksheet
Dim w As Window

For Each wks In ActiveWorkbook.Windows(1).SelectedSheets
Application.ScreenUpdating = False
Range("A1").Select
Set w = ActiveWindow
For icht = 1 To ActiveSheet.ChartObjects.Count
w.ScrollRow = ActiveSheet.ChartObjects(icht).TopLeftCell.Row
ActiveSheet.ChartObjects(icht).Activate
ActiveChart.Pictures.Insert ("D:\Bao Lan Dieu's files\My Pictures\PI logo\CI-icon.jpg")
Next
w.ScrollRow = 1
Set w = Nothing
Application.ScreenUpdating = True
Next
End Sub

yurble_vn
06-27-2007, 02:34 AM
Solved, although it's a little stupid way
Sub Insert_Pic()
Dim icht As Integer
Dim ishp As Integer
Dim wks As Worksheet
Dim w As Window

For Each wks In ActiveWorkbook.Windows(1).SelectedSheets
Application.ScreenUpdating = False
Range("A1").Select
Set w = ActiveWindow
For icht = 1 To ActiveSheet.ChartObjects.Count
w.ScrollRow = ActiveSheet.ChartObjects(icht).TopLeftCell.Row
ActiveSheet.ChartObjects(icht).Activate
For ishp = 1 To ActiveChart.Shapes.Count
ActiveChart.Shapes(ishp).Select
Next
ActiveChart.ChartArea.Select
ActiveChart.Pictures.Insert ("D:\Bao Lan Dieu's files\My Pictures\PI logo\CI-icon.jpg")
Next
w.ScrollRow = 1
Set w = Nothing
Application.ScreenUpdating = True
Next
End Sub

jtsee2
03-15-2011, 09:44 AM
Hello,
I have been using this code for 6 months, but today it starts to give me the following message:
Run-time error '-2147417848 (80010108)':
Method 'Insert' of object 'Pictures' failed

It's failing at:
ActiveChart.Pictures.Insert Dest
I've also tried
ActiveChart.Pictures.Insert(Dest).Select

What I have is:
1. A chart on a page by itself. I run a code that generates the graphs on the chart.
2. There is an image in the top right of the chart area.
3. This code deletes the current image and then inserts the new image.
4. Formats the new image: size, crop and transparency.

I have done a google search looking for answers and this is the closest match I could find. Please help.


Sub SaveCharts_StartNew()
ActiveSheet.Unprotect
Sheets("WindGust").Shapes(1).Delete
blueScreen = RGB(255, 255, 255) 'transparency color
Dest = Worksheets("WRC").Range("L20") 'file name
ActiveChart.ChartArea.Select
ActiveChart.Pictures.Insert Dest
With Sheets("WindGust").Shapes(1)
With .PictureFormat
.TransparentBackground = True
.TransparencyColor = blueScreen
.CropTop = 40
End With
.LockAspectRatio = msoFalse
.Width = 125
.Height = 113
.Fill.Visible = False
.IncrementTop -40#
.IncrementLeft -60
End With
'do other events
End Sub