Exporting Cell Range As PNG - Getting A White Border To The Bottom & Right - Why?

TkdKidSnake

Active Member
Joined
Nov 27, 2012
Messages
255
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am exporting a range of cells as a PNG however, when I look at the result I am getting a white border at the bottom and to the right (there isn't any white there) - Any ideas why and how to resolve this as it would save me having to manually edit each time.

The code I have is:

VBA Code:
Sub Xport01Mon()
'
Sheets("Mon").Select
ActiveSheet.Unprotect

Dim ws As Worksheet
Dim table As Range
Dim pic As Picture
Dim cht As ChartObject
Dim myPath As String
Dim myPic As String
Dim myWidth As Long
Dim myHeight As Long

Set ws = ThisWorkbook.Sheets("Mon")
Set table = ws.Range("B5:X548")

myPath = "C:\Users\stevseat\Documents\001 - TST Schedule\"
myPic = Range("C1").Value

table.CopyPicture xlScreen, xlPicture
myWidth = table.Width
myHeight = table.Height

Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
    Width:=myWidth, Height:=myHeight)
    
cht.Activate

With cht.Chart
    .Paste
    .Export Filename:=myPath & myPic, Filtername:="png"
End With

cht.Delete

ActiveSheet.Protect

End Sub

This is what I am seeing as the output:

2024 - Wk 48 - 01 - Monday.png


Any help you can provide would be greatly appreciated.

Thanks in advance
 
Try xlContinuous instead.
Hi, thank you for your help.

Unfortunately this didn't make a difference:

changing from:
VBA Code:
cht.Border.LineStyle = xlLineStyleNone

changing to:
VBA Code:
cht.Border.LineStyle = xlContinuous
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If you want to try by installing IrfanView, it is a very good program and it is free, with all the required plugins.

This is for two computers as you see.
Normally one does not need the Select Case part.

Code:
Sub With_IrfanView()
Dim myPic As Object
Dim cv As String, savePath As String
Set myPic = ActiveSheet.Range("A1:H1")    '<---- Change as required
Select Case Environ("computername")
Case "DESKTOP"
cv = "C:\Program Files\IrfanView\i_view64.exe"
Case "LAPTOP"
cv = "C:\Program Files (x86)\IrfanView\i_view32.exe"
End Select
savePath = "C:\WhatEver Folder\WhatEver Picture Save Name.jpg"    '<---- Change as required
myPic.Copy
Shell cv & " ""/clippaste /convert=" & savePath & """", vbNormalFocus
End Sub
Thanks for this I will have to try on another computer/laptop as I can only install from the Microsoft Store on this Laptop.
 
Upvote 0
Try xlContinuous instead.
The below between the dashed lines seems to have solved my issue - its the chart area that's creating what looks like a border.

VBA Code:
Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
    Width:=myWidth, Height:=myHeight)
'---------------------------------------------------
cht.ShapeRange.Fill.Visible = False
cht.ShapeRange.Line.Visible = False
'---------------------------------------------------
 
Upvote 0
Thanks for this I will have to try on another computer/laptop as I can only install from the Microsoft Store on this Laptop.
The below between the dashed lines seems to have solved my issue - its the chart area that's creating what looks like a border.

VBA Code:
Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
    Width:=myWidth, Height:=myHeight)
'---------------------------------------------------
cht.ShapeRange.Fill.Visible = False
cht.ShapeRange.Line.Visible = False
'---------------------------------------------------
 
Upvote 0
If you want to try by installing IrfanView, it is a very good program and it is free, with all the required plugins.

This is for two computers as you see.
Normally one does not need the Select Case part.

Code:
Sub With_IrfanView()
Dim myPic As Object
Dim cv As String, savePath As String
Set myPic = ActiveSheet.Range("A1:H1")    '<---- Change as required
Select Case Environ("computername")
Case "DESKTOP"
cv = "C:\Program Files\IrfanView\i_view64.exe"
Case "LAPTOP"
cv = "C:\Program Files (x86)\IrfanView\i_view32.exe"
End Select
savePath = "C:\WhatEver Folder\WhatEver Picture Save Name.jpg"    '<---- Change as required
myPic.Copy
Shell cv & " ""/clippaste /convert=" & savePath & """", vbNormalFocus
End Sub
Hi,

I've managed to solve this, it would appear its the chart background that was causing the border issue - see the below between the dashes as this has now removed the border.

VBA Code:
Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
    Width:=myWidth, Height:=myHeight)
'---------------------------------------------------
cht.ShapeRange.Fill.Visible = False
cht.ShapeRange.Line.Visible = False
'---------------------------------------------------
cht.Activate
 
Upvote 0
MY full code now looks like this is anyone is interested or has the same issue.

VBA Code:
Sub Xport01Mon()
'
Sheets("Mon").Select
ActiveSheet.Unprotect

Dim ws As Worksheet
Dim table As Range
Dim pic As Picture
Dim cht As ChartObject
Dim myPath As String
Dim myPic As String
Dim myWidth As Long
Dim myHeight As Long

Set ws = ThisWorkbook.Sheets("Mon")
Set table = ws.Range("B5:X548")

myPath = "C:\Users\stevseat\Documents\001 - TST Schedule\"
myPic = Range("C1").Value

table.CopyPicture xlScreen, xlPicture
myWidth = table.Width
myHeight = table.Height

Set cht = ws.ChartObjects.Add(Left:=0, Top:=0, _
    Width:=myWidth, Height:=myHeight)
'---------------------------------------------------
cht.ShapeRange.Fill.Visible = False
cht.ShapeRange.Line.Visible = False
'---------------------------------------------------
cht.Activate
cht.Border.LineStyle = 0

With cht.Chart
    .Paste
    .Export Filename:=myPath & myPic, Filtername:="png"
End With

cht.Delete

ActiveSheet.Protect

End Sub
 
Upvote 0
Like John_w, thumbs up and thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top