Solved

How do I copy a slide layout in PPT 2007 in VBA

Posted on 2009-07-16
4
1,519 Views
Last Modified: 2012-05-07
I am trying to copy over a slide from one presentation to another. Most of the code works but I the text boxes are skewing slightly off and the background is not formatting correctly either (picture is sliding off the right hand side of the page).

Please let me know how I can fix this.

Thanks,
Dekka...
Sub PullExcelData()

    

    mbErrorSwitch = False

    Set objWorksheet = objWorkbook.Worksheets("QueryMap")

    PresName = objWorksheet.Range("nrPresName").Value

    strPresPath = WKBKPATH & PresName

    oPPTObjName = objWorksheet.Range("nrObjName").Value

    

    Application.DisplayAlerts = False

    

    'Open the presentation

    Presentations.Open (strPresPath)

    'Set oPPTApp = CreateObject("Powerpoint.Application")

    Set objPresentation = ActivePresentation

    Set objSlide = objPresentation.Slides(1)

    

'    Set objexcel = CreateObject("Excel.Application")

'    objexcel.Visible = False

    

'    Set objWorkbook = objexcel.Workbooks.Open(ActivePresentation.Path & "\Connection Script1.2.xlsm")

    Set objWorksheet = objWorkbook.Worksheets("Main")

    Set objRange = objWorksheet.Range("nrMainCopyRange")

    

    'Instantiate the workbook object from the OLE object

    Set objExcelDestWb = objSlide.Shapes(oPPTObjName).OLEFormat.Object

    

    'Define what worksheet to paste into

    Set objExcelDestSheet = objExcelDestWb.Worksheets(1)

    

    'Optionally clear destination sheet, depends what you are updating..

    objExcelDestSheet.Cells.ClearContents

    objWorksheet.Activate

    objRange.Copy

    

    'PASTE INTO EXCEL TABLE at cell A1

    objExcelDestSheet.Paste objExcelDestSheet.Range("A1")

        

   ' AutoFitColumns

    Dim mvgRng As Range

    With objExcelDestSheet

        .Columns("C:Z").AutoFit

    End With

    

    objExcelDestSheet.Range("A1").EntireColumn.ColumnWidth = 50

    Set mvgRng = objExcelDestSheet.Range("B1:Z1")

    

    For Each c In mvgRng.Cells

        

        If c.EntireColumn.ColumnWidth > 30 Then

            c.EntireColumn.ColumnWidth = 30

        Else: End If

    Next c
 

    objExcel.CutCopyMode = False

    Application.DisplayAlerts = True

        

    On Error Resume Next

    Set oPPTShape = objPresentation.Slides(1).Shapes("txtAsOfDate")

        Set objWorksheet = objWorkbook.Worksheets("SysConfig")

        oPPTShape.TextFrame.TextRange.Text = "As of " & objWorksheet.Range("nrCharDateDisplay").Value

    

    Set oPPTShape = objPresentation.Slides(1).Shapes("txtFootnote")

        Set objWorksheet = objWorkbook.Worksheets("Other")

        oPPTShape.TextFrame.TextRange.Text = objWorksheet.Range("nrFootnote").Value

    

'    oPPTApp.Visible = True

'    SlideCopy2
 

            Dim s As Slide

            Dim vt As Long

            

            On Error GoTo slideError

            

            Presentations.Add

            Set objPresentation2 = ActivePresentation
 

            vt = objPresentation2.Windows(1).ViewType

           objPresentation2.Windows(1).ViewType = ppViewOutline

            For Each s In objPresentation.Slides

                s.Copy

                Set cl = s.CustomLayout

                objPresentation2.Slides.Paste

                objPresentation2.Slides(objPresentation2.Slides.Count).CustomLayout = cl

            Next s

            objPresentation2.Windows(1).ViewType = vt

        

    SlideShowWindows(Index:=1).View.Exit

    

        Exit Sub

        

slideError:

     

End Sub

Open in new window

0
Comment
Question by:DekkaG
  • 2
4 Comments
 
LVL 2

Expert Comment

by:SaWey
ID: 24867140
What you can do is create a macro which does what you want and then look at it's source code to see how you can do it in VBA.
0
 
LVL 7

Accepted Solution

by:
hippohood earned 500 total points
ID: 25225384
In line 83, if I understand properly, you are re-applying the slide layout AFTER the content is inserted.
Make sure that you apply same design template in new and original presentations. In case if your original slide layout has been slightly modified by user, which often happens when user is trying to fit text in, this MIGHT lead to recovering original template layout, not the current layout. Also, if textboxes were used instead of text frame, your layout would be screwed up either.
If nothing above is true or is too complicated to resolve you may just "manually" setup the shape factors and position for the objects in the new slide.
The code will look as below (I am adding extra lines only to 79-84 from your orignial sniplet)

            For Each s In objPresentation.Slides

                s.Copy

                Set cl = s.CustomLayout

                objPresentation2.Slides.Paste

                objPresentation2.Slides(objPresentation2.Slides.Count).CustomLayout = cl

' inserted here

    Set s2 = objPresentation2.Slides(objPresentation2.Slides.Count)

    For i = 1 To s.Shapes.Count

        s2.Shapes(i).Left = s.Shapes(i).Left

        s2.Shapes(i).Top = s.Shapes(i).Top

        s2.Shapes(i).Width = s.Shapes(i).Width

        s2.Shapes(i).Height = s.Shapes(i).Height

        '... other properties, like text frame margin...

    Next aShape
 
 

            Next s

            objPresentation2.Windows(1).ViewType = vt

Open in new window

0
 

Author Closing Comment

by:DekkaG
ID: 31604132
Sorry it took me so long to get back to you. Personal issues came up that had me away from work.

Thanks for the help.
0
 
LVL 7

Expert Comment

by:hippohood
ID: 25585156
Hope everything is well with you now.
Thanks, for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting the Scene PowerPoint is a creative tool in the right hands but it also includes a much underutilised programming dimension. In this beginner level article, we're going to show you some of some key elements of programming PowerPoint using th…
 Regular Expressions Microsoft Word has sophisticated search tools that can search for patterns. For example if you wanted to search for all UK phone numbers that followed a pattern of five digits, a space and then six digits you can easily do th…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now