Consulting

Results 1 to 8 of 8

Thread: Help: how to insert pic, textbox to chart automatically???

  1. #1

    Help: how to insert pic, textbox to chart automatically???

    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 :

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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    [VBA]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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    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.

  4. #4
    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?

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is better, but I'm needing to use BringToFront on one symbol
    [VBA]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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    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.

    [VBA]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[/VBA]

  7. #7
    Solved, although it's a little stupid way
    [VBA]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[/VBA]

  8. #8
    VBAX Newbie
    Joined
    Mar 2011
    Posts
    1
    Location
    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.

    [VBA]
    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
    [/VBA]

Posting Permissions

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