• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Export pictures in worksheet to adjacent folder in the same path

YasserKhalil

Well-Known Member
Hello everyone
I have a workbook that was working well and exports pictures in the sheet to the same workbook path but now I go blank pictures in the exported files
Code:
Sub ExportPictures()
    Dim imag_path As String, shap As Shape
    Dim f_name As String, x As Integer
   
    x = 1
    imag_path = ThisWorkbook.Path
   
    For Each shap In ActiveSheet.Shapes
        If shap.Type = msoPicture Then
            f_name = imag_path & "\" & x & ".jpg"
            CopyPicToFile shap, f_name
            x = x + 1
        End If
    Next shap
End Sub

Sub CopyPicToFile(aShape As Shape, aFilename As String)
    Dim cht As Excel.ChartObject
   
    Application.ScreenUpdating = False
        aShape.CopyPicture xlScreen, xlPicture
        Set cht = ActiveSheet.ChartObjects.Add(0, 0, aShape.Width, aShape.Height)
        cht.Chart.Paste
        cht.Chart.Export aFilename
        cht.Delete
    Application.ScreenUpdating = True
   
    Set cht = Nothing
End Sub
I am using office 2016 32 bit ... In the past I was using office 2013 64 bit ..
 

Attachments

  • Export Picture From Sheet.xlsm
    122.6 KB · Views: 5
It runs fine for me in both Excel 2010 (32 bit) and Excel 2016 (64 bit) both under Windows 10.

It saves the two images in the folder where I saved the file

Have you stepped through the macro line by line using F8?
check everything as you go
 
Thanks Mr. Hui for reply
I checked the code step by step and nothing wrong is happening and after each step I examine the workbook path and the chart .. In this case I can get the pictures exported if I stepped one by one and that is very weird ..
But if I executed the code to get the pictures exported .. I got blank files (blank images)
 
Try adding delay at different point(s) in code and see what happens. If it does give correct result when you step one by one. It leads me to believe, at some point in code, code is doing operation before previous step finishes.

Time delay code. Adjust TimeValue as needed.
Code:
Application.Wait(Now + TimeValue("00:00:01"))
 
Thanks a lot Mr. Chihiro for reply
I tried the line of waiting more than 5 times at different positions but the same problem

I noticed that when stepping the code when I reached this line and the line is in yellow
Code:
cht.Chart.Export aFilename
then navigated to the sheet and return to the vbe and resumed stepping the code I got my result well
 
Hmm, tested and working without issue for me.

FYI - When you step through with F8, it should highlight the line being executed in yellow.
 
Thanks a lot
I know it is working .. I was working on it before and it was working very well
But now after windows 7 32 bit and office 2016 32 bit it doesn't work
As for highlighting the line to be yellow I know it is executed after .. but I noticed after the line of paste it is ok on the chart and the picture pasted .. returned to the code and executed the line of export it works well in this case ...
But if didn't activate the sheet it does export blank picture
 
Then instead of using ActiveSheet in your code replace it with Sheet index reference and see what happens.

I ran test on following versions and all worked fine.
Windows 7 64bit - Excel 2010 (32 bit)
Windows10 64bit - Excel 2013 (32 bit)
Windows 7 64bit - Excel 2016 (32 bit)
 
I tied both Sheets("Sheet1") and Sheet1 and the same weird problem
It seems to me the problem with office 2016 version ..
This is my version:
en_office_professional_plus_2016_x86_x64_dvd_6962141
 
Back
Top