Search code examples
vbapowerpointvertical-alignment

I need Help, in Aligning copied items to Powerpoint from Excel


Sub ToPowerPoint()
Dim pApp As PowerPoint.Application
Dim pSilde As PowerPoint.Slide
Dim pPres As PowerPoint.Presentation

'I have declared here pp Objects

Application.EnableEvents = False
Application.ScreenUpdating = False

'Turning off screen updating

Set pApp = New PowerPoint.Application
pApp.Visible = True

'Setting up new pp instance

pApp.Activate
Set pPres = pApp.Presentations.Add
Set pSlide = pPres.Slides.Add(1, ppLayoutBlank)

'Adding up new presentation and slide within pp

pSlide.Select    
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Set wb = Workbooks("BC_WTB__DRAFT.xlsb")
'setting up here xl workbooks
'
wb.Activate
Worksheets("BS").Visible = True
Worksheets("BS").Select
Worksheets("BS").Range("G5:H5").Select
Selection.Copy
pSlide.Shapes.PasteSpecial ppPasteRTF
**'copying some cell values here and below**
Worksheets("BS").Range("G12").Select
Selection.Copy
pSlide.Shapes.PasteSpecial ppPasteEnhancedMetafile
pApp.ActiveWindow.Selection.ShapeRange.Top = 193

Worksheets("BS").Range("H12").Select
Selection.Copy
pSlide.Shapes.PasteSpecial ppPasteEnhancedMetafile
pApp.ActiveWindow.Selection.ShapeRange.Top = 193
'I need to align pasted objects in one horizontal line
'without using Top or Left methods

'I need help after copied cells are pasted in pp, to align them in 'some vertical and horizontal line without using top or left method.

End Sub

Solution

  • ExcelHelp.

    If you are trying to aling objects in powepoint presentation you can use the native alingment and distribute functions.

    Just adding this at the and of your code.

    Dim myArray() As Variant, myRange As Object
    
    myArray = Array("img1", "img2", "txt1")
    
    Set myRange = ActivePresentation.Slides(1).Shapes.Range(myArray)
    
        myRange.Distribute msoDistributeHorizontally, msoFalse
        myRange.Distribute msoDistributeVertically, msoFalse
        myRange.Align msoAlignLefts, msoFalse
    

    You just need to change names of the defaut shapes in the array (names change because we have different languages).

    If you need to aling left, or top or right use the last line, or distribute heights / widths