简体   繁体   中英

Using Excel VBA to change PowerPoint Slide Size - Late Binding

I'm new-ish to VBA and brand new to StackOverflow, so please forgive any breaches of etiquette. I have a project to create a tool using Excel VBA that will allow the user to identify a source Excel file and then copy the print area of every worksheet in the Excel file into a slide in a newly created PowerPoint presentation. I have to use late binding because I can't assume the users of the tool will all be using the same versions of the Microsoft Office products, or that they will have enabled the reference to the PowerPoint Object Library. I have managed to do all of this. Where I am getting hung up is when I try to set the size of the slides - I have been asked for the slides to be in the 4:3 slide size available in PowerPoint 2013 (or a similar ratio if needed to make the tool work in older versions of PowerPoint - I have been trying for ppSlideSizeLetterPaper). This is throwing "Run-time error '438': Object doesn't support this property or method". If anyone could offer suggestions as to how to get around this, i'd be grateful.

Here's the relevant snippets of the code:

Public PowerPointApp As Object
Public TargetFile As Object
…
Sub Master()
Set PowerPointApp = CreateObject("PowerPoint.Application")
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
If Err.Number = 429 Then
    MsgBox "PowerPoint could not be found.  Exiting macro."
    Exit Sub
End If
Set TargetFile = PowerPointApp.Presentations.Add
    PowerPointApp.Visible = True
    PowerPointApp.Activate
…<code to add slides and paste data from Excel into the slides>
PowerPointApp.TargetFile.PageSetup.SlideSize = 2    ‘this is where the error is thrown.  2 is the numeric equivalent of ppSlideSizeLetterPaper

Targetfile is undefined in that context - there is no method or property called that on the application object.

As you've already have an object of that name, just use that.

TargetFile.PageSetup.SlideSize = 2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM