excelvbapowerpoint

Remove border between Excel and PPT for a copy and paste using VBA


I copy and paste between Excel and PPT using VBA.

The PastePicture creates grey borders that I do not want.

How do I remove those borders?

Sub CopyAndPaste()

Dim PPTApp As PowerPoint.Application
Dim PPTShape As PowerPoint.Shape
Dim PPTFile As PowerPoint.Presentation
Dim mySlide As Object

Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String

strPresPath = "C:\Users\xxxxxxxx"
strExcelFilePath = "C:\Users\pppppppp"
    
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Visible = msoTrue
    
Set PPTFile = PPTApp.Presentations.Open(strPresPath)
    
'Step 1 Copy the data
Workbooks("Globalyyyy").Worksheets("ILLUSTRATIONS CHARTS").Range("B58:G69").CopyPicture

Application.CutCopyMode = False

'Step 2 Paste the data
Set mySlide = PPTFile.Slides(5)

With mySlide
mySlide.Shapes.PasteSpecial Paste:=xlPastePicture 'DataType:=ppEnhancedMetafile
End With

Application.DisplayAlerts = False
Application.DisplayAlerts = True

Solution

  • As @Haluk mentioned, you have to "erase" the cell borders by drawing them as the background color. The code below is an example for how to accomplish this.

    I've included a bonus function for attaching to PowerPoint that I find very useful.

    Option Explicit
    
    Sub CopyAndPaste()
        Dim PPTApp As PowerPoint.Application
        Set PPTApp = AttachToMSPowerPointApplication
        PPTApp.Visible = msoTrue
        
        Dim PPTFile As PowerPoint.Presentation
        Set PPTFile = PPTApp.Presentations.Add
        
        Dim mySlide As Object
        PPTFile.Slides.Add 1, ppLayoutText
        Set mySlide = PPTFile.Slides(1)
        
        Dim myData As Range
        Set myData = ThisWorkbook.Worksheets("ILLUSTRATIONS CHARTS").Range("B58:G69")
        
        'Step 0 - "erase" the grid lines
        With myData.Borders
            '--- you can optionally save the existing setting for the borders,
            '    then return them to their original state when you're done
            Dim ogLineStyle As XlLineStyle
            Dim ogColor As XlColorIndex
            Dim ogWeight As Long
            ogLineStyle = .LineStyle
            ogWeight = .Weight
            ogColor = .Color
            
            .LineStyle = xlContinuous
            .Color = myData.Interior.Color 'set to same as background color
            .Weight = xlThin
        
            'Step 1 Copy the data
            myData.CopyPicture
            Application.CutCopyMode = False
    
            'Step 2 Paste the data
            mySlide.Shapes.PasteSpecial
            
            'Step 3 restore the original borders
            .LineStyle = ogLineStyle
            .Color = ogColor
            .Weight = ogWeight
        End With
    
    End Sub
    
    Public Function AttachToMSPowerPointApplication() As PowerPoint.Application
        '--- finds an existing and running instance of MS PowerPoint, or starts
        '    the application if one is not already running
        Dim msppApp As PowerPoint.Application
        On Error Resume Next
        Set msppApp = GetObject(, "PowerPoint.Application")
        If Err > 0 Then
            '--- we have to start one
            '    an exception will be raised if the application is not installed
            Set msppApp = CreateObject("PowerPoint.Application")
        End If
        Set AttachToMSPowerPointApplication = msppApp
    End Function