+ Reply to Thread
Results 1 to 5 of 5

Picture Export Not Functional from Button

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Picture Export Not Functional from Button

    Hello All,

    The issue I am having is that my code works perfectly when I step through it in the VBA window but when I assign the same macro to a button and run it from the sheet it doesn't work properly.

    I am copying a group and pasting it as a chart object for exporting as an image, but when I am running it off a button the image is blank. When I run each step in the VBA window it exports with the image in it properly.

    My code is as follows:
    Sub export_linear()
    Dim myFileName As String
    Dim fullpathandfilename As String
    Dim mypath As String
    Dim mygroup As Object
    Dim chtObj As Object
    
    Application.ScreenUpdating = False
    mypath = ActiveWorkbook.path
    
    Set mygroup = Worksheets("Linear_Gauge").Shapes("Group 17")
    mygroup.CopyPicture
    
    Set chtObj = ActiveSheet.ChartObjects.Add(mygroup.Left, mygroup.Top, mygroup.Width, mygroup.Height)
    
    chtObj.Name = "Grouped 2"
    chtObj.Chart.Paste
    
    ActiveSheet.Shapes("Grouped 2").ScaleWidth 1.75, msoFalse, msoScaleFromTopLeft
    ActiveSheet.Shapes("Grouped 2").ScaleHeight 1.75, msoFalse, msoScaleFromTopLeft
    
    myTime = Format(TimeValue(Now), "hh.mm.ss am/pm")
    myFileName = Range("O2").Value & " (" & myTime & ").png"
    fullpathandfilename = ThisWorkbook.path & "\" & myFileName
    
    On Error Resume Next
    Kill (path)
    chtObj.Chart.export Filename:=fullpathandfilename, Filtername:="PNG"
    
    chtObj.Delete
    Set chtObj = Nothing
    
    Application.ScreenUpdating = True
    
    End Sub
    It seems to not be recognizing/performing the "chtObj.Chart.Paste" operation. I have attempted to add some wait operations in there in case it was running through lines faster than my computer could keep up, that's also why I have screenupdating = False.

    Any help on how to resolve this issue would be greatly appreciated.

    Thank you.
    Last edited by DarkF1ame; 06-22-2017 at 07:54 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Picture Export Not Functional from Button

    Your code works for me even when running from a button. So I'm not sure what the problem is. I seriously doubt the code is running too fast for your computer. VBA doesn't work like that.

    Here's your same code cleaned up a bit. I didn't change any methods, but maybe it somehow it fixes the issue.

    Sub export_linear()
        Dim myFileName As String
        
        Application.ScreenUpdating = False
        
        With Worksheets("Linear_Gauge").Shapes("Group 17")
        
            .CopyPicture
            
            With ActiveSheet.ChartObjects.Add(.Left, .Top, .Width, .Height)
                
                .Chart.Paste
                .ShapeRange.ScaleWidth 1.75, msoFalse, msoScaleFromTopLeft
                .ShapeRange.ScaleHeight 1.75, msoFalse, msoScaleFromTopLeft
                
                myFileName = Range("O2").Value & Format(Now, " (hh.mm.ss am/pm)") & ".png"
                .Chart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"
                .Delete
                
            End With
        End With
        
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Re: Picture Export Not Functional from Button

    I have inserted this code to my spreadsheet and it continues to not function when fired from a button.

    I recently upgraded to Office Standard 2016, do you think that may have any bearing on the situation?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Picture Export Not Functional from Button

    Quote Originally Posted by DarkF1ame View Post
    I have inserted this code to my spreadsheet and it continues to not function when fired from a button.

    I recently upgraded to Office Standard 2016, do you think that may have any bearing on the situation?
    I found this thread with the same issue.
    https://www.mrexcel.com/forum/excel-...mpty-file.html

    Bottom line; he says Activating the chart cured the problem.

    Sub export_linear()
        Dim myFileName As String
        
        Application.ScreenUpdating = False
        
        With Worksheets("Linear_Gauge").Shapes("Group 17")
        
            .CopyPicture
            
            With ActiveSheet.ChartObjects.Add(.Left, .Top, .Width, .Height)
                
                .Chart.Paste
                .ShapeRange.ScaleWidth 1.75, msoFalse, msoScaleFromTopLeft
                .ShapeRange.ScaleHeight 1.75, msoFalse, msoScaleFromTopLeft
                
                myFileName = Range("O2").Value & Format(Time, " (hh.mm.ss am/pm)") & ".png"
                .Activate
                .Chart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"
                .Delete
                
            End With
        End With
        
        Application.ScreenUpdating = True
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Re: Picture Export Not Functional from Button

    AlphaFrog,

    A variation of your suggestion worked perfectly!

    I had to put it in before the paste function for it to work, but that did it!

    With ActiveSheet.ChartObjects.Add(.Left, .Top, .Width, .Height)
                
                .Activate
                .Chart.Paste
                .ShapeRange.ScaleWidth 1.75, msoFalse, msoScaleFromTopLeft
                .ShapeRange.ScaleHeight 1.75, msoFalse, msoScaleFromTopLeft

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Export picture from sheet
    By Temporary-Failure in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2016, 12:17 PM
  2. [SOLVED] Insert Picture in cell, click commanbutton to rename picture, lock cell and remove button
    By thecdnmole in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-28-2014, 10:05 PM
  3. Hide a picture button placed in all sheets using a option button (form control)
    By lagaranch in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-26-2012, 10:33 AM
  4. Export picture containing both chart and range...
    By asgersax in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 04:56 AM
  5. export and save worksheet as a picture.
    By Diesel13 in forum Excel General
    Replies: 18
    Last Post: 10-22-2010, 05:04 PM
  6. Export as picture
    By tommy_gtr in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-02-2005, 06:20 PM
  7. export range as picture
    By manolo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2005, 06:05 PM

Tags for this Thread

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