Reputation: 110
I am creating a PPT from an Excel-VBA file and want to set the zoom of the ppt to 100%.
How can I do this from the Excel file without activating the PowerPoint window?
Or how can I even do it with activating the window?
The following example code in excel opens a ppt file on the Desktop and tries to set the zoom of this ppt but it seems not to work:
Sub CreatePptAndSetZoom()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim strTemplate$
'Look for existing instance of PPT
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance of PPT if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Define Path of Template and open new ppt
strTemplate = Environ("UserProfile") & "\Desktop\Test.pptx"
Set ppPres = ppApp.presentations.Open(strTemplate, False, True, True)
'Set Zoom
ppApp.ActiveWindow.View.Zoom = 100
End Sub
Setting the Zoom from the Excel file in the Excel file works with the slightly different statement (without View
):
Application.ActiveWindow.Zoom = 100
Does anybody know how to solve this problem without any code in the ppt file?
Upvotes: 1
Views: 1122
Reputation: 110
With some help (Thank you R3uK!) I found the following solution based on the last code in the question:
Solution:
Instead of ppApp.ActiveWindow.View.Zoom = 100
use the following:
ppPres.Windows(1).Panes(2).Activate
--> Activate the main slide pane
ppPres.Windows(1).View.Zoom = 100
--> Set the zoom of the active pane in the window
Some remarks:
ppPres.Windows(1)
-> Collection with only the window where the presentation is shown in
ppApp.Windows(1)
-> Collection with all opened ppt windows. So if a ppt was opened before item 1 this would not return the right window.
ppPres.Windows(1).Panes(2)
-> Collection of panes inside of the ppt window which are in my case the following ViewTypes dependent from the item number:
Item 1: ppViewThumbnails
(miniature slides on the left)
Item 2: ppViewSlide
(main view)
Item 3: ppViewNotesPage
(comment section on the bottom)
So in my example I wanted to change the zoom of Item 2 the main slide view pane.
Upvotes: 2
Reputation: 14537
Your code run smoothly using late biding :
Sub ZoomInPptFromExcel()
Dim ppApp As Object
'Get existing instance of PPT
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Set Zoom
ppApp.ActiveWindow.View.Zoom = 100
End Sub
Sub CreatePptAndSetZoom()
Dim ppApp As Object 'PowerPoint.Application
Dim ppPres As Object 'PowerPoint.Presentation
Dim strTemplate$
'Look for existing instance of PPT
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance of PPT if no instance exists
If ppApp Is Nothing Then Set ppApp = CreateObject("PowerPoint.Application")
'Define Path of Template and open new ppt
strTemplate = Environ("UserProfile") & "\Desktop\Test.pptx"
Set ppPres = ppApp.Presentations.Open(strTemplate, False, True, True)
'Set ppPres = ppApp.Presentations.Add
'Set Zoom
ppApp.ActiveWindow.View.Zoom = 100
End Sub
Upvotes: 1