Export print area as picture

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Export print area as picture

Post by YasserKhalil »

Hello everyone

I am trying to export the print area as picture. I have tried some codes and some works well but I have a little problem
The print area is $A$11:$I$26 and I need to include the headers in A1:I1 (the title rows) to be in the same picture that will be exported
Any idea how to do that?
This is my try till now but O got error of not being able to deal with multiple selections

Code: Select all

Sub pic_save()
    Dim PicPath As String
    Dim OutPutPath As String
    Dim wS As Worksheet
    Set wS = ThisWorkbook.Sheets("Sheet1")
    OutPutPath = ThisWorkbook.Path & "\"
'wS.Range(wS.PageSetup.PrintArea).Address

Dim s As String
s = Union(wS.Range("A1:I1"), wS.Range(wS.PageSetup.PrintArea)).Address

    PicPath = Generate_Image_From_Range(wS, s, OutPutPath, "pic", "png", False)
    MsgBox wS.Name & " exported to : " & vbCrLf & _
            PicPath, vbInformation + vbOKOnly
End Sub

Public Function Generate_Image_From_Range(wS As Worksheet, _
                                        RgStr As String, _
                                        OutPutPath As String, _
                                        ImgName As String, _
                                        ImgType As String, _
                                        Optional TrueToTuneFilters As Boolean = False) As String
    Dim ImgPath As String
    Dim oRng As Range
    Dim oChrtO As ChartObject
    Dim lWidth As Long, lHeight As Long
    Dim ActSh As Worksheet
    Dim ValScUp As Boolean
    ImgPath = OutPutPath & ImgName & "." & ImgType
    Set ActSh = ActiveSheet
    Set oRng = wS.Range(RgStr)

    wS.Activate
'On Error GoTo ErrHdlr
    With oRng
        .Select
        '''Zoom to improve render
        ValScUp = Application.ScreenUpdating
        Application.ScreenUpdating = False
        ActiveWindow.Zoom = True
        DoEvents
        Application.ScreenUpdating = ValScUp

        lWidth = .Width
        lHeight = .Height
        .CopyPicture xlScreen, xlPicture        'Best render
    End With 'oRng


    Set oChrtO = wS.ChartObjects.Add(Left:=0, Top:=0, Width:=lWidth, Height:=lHeight)
    With oChrtO
        .Activate
        .Chart.Paste
        With .ShapeRange
            .Line.Visible = msoFalse
            .Fill.Visible = msoFalse
            With .Chart.Shapes.Item(1)
                .Line.Visible = msoFalse
                .Fill.Visible = msoFalse
            End With '.Chart.Shapes.Item (1)
        End With '.ShapeRange
        With .Chart
            DoEvents
            .Export Filename:=ImgPath, Filtername:=ImgType, Interactive:=TrueToTuneFilters
'            If Not TrueToTuneFilters Then _
'                .Export filename:=ImgPath, Filtername:=ImgType, Interactive:=False
'            If TrueToTuneFilters Then _
'                .Export filename:=ImgPath, Filtername:=ImgType, Interactive:=True
        End With '.Chart
        DoEvents
        .Delete
    End With 'oChrtO
    ActSh.Activate

    Generate_Image_From_Range = ImgPath
On Error GoTo 0
Exit Function
ErrHdlr:
Generate_Image_From_Range = vbNullString
End Function


User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export print area as picture

Post by HansV »

You'd have to hide rows 2 to 10 and export A1:I26.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Export print area as picture

Post by YasserKhalil »

The problem is that the print area will not be fixed as it is changed by specific code ..so how can the rows between the first row and the print area be hidden based on the first row of the print area? ..

Also I hide thew rows manually and retry the code but I got the same error ..

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export print area as picture

Post by HansV »

You can determine the rows to hide from the PrintArea:

Code: Select all

Sub pic_save()
    Dim PicPath As String
    Dim OutPutPath As String
    Dim wS As Worksheet
    Dim s As String
    Dim r As Range
    Set wS = ThisWorkbook.Sheets("Sheet1")
    OutPutPath = ThisWorkbook.Path & "\"
    Set r = wS.Range(Split(wS.PageSetup.PrintArea, ":")(0)).Offset(-1)
    Range("A2:" & r.Address).EntireRow.Hidden = True
    s = "A1:" & Split(wS.PageSetup.PrintArea, ":")(1)
    PicPath = Generate_Image_From_Range(wS, s, OutPutPath, "pic", "png", False)
    Range("A2:" & r.Address).EntireRow.Hidden = False
    MsgBox wS.Name & " exported to : " & vbCrLf & _
            PicPath, vbInformation + vbOKOnly
End Sub
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Export print area as picture

Post by YasserKhalil »

Thanks a lot for great help. That works well
The code affects the zoom but I didn't find the lines that can control the zoom as it is changed after running the code

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export print area as picture

Post by HansV »

The line

Code: Select all

        ActiveWindow.Zoom = True
sets the zoom percentage so that the selection fills the window. You can reset it to 100% using

Code: Select all

        ActiveWindow.Zoom = 100
at the end of the function, or you can store the original zoom percentage in a variable and use that to restore this percentage.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Export print area as picture

Post by YasserKhalil »

Thank you very much my tutor
Best Regards