2017-03-21 41 views
1

我從Excel-VBA文件創建PPT,並且想要將PPT的縮放設置爲100%。設置在不激活的情況下放大PowerPoint文檔窗口

如何在不激活PowerPoint窗口的情況下從Excel文件執行此操作?
或者我怎麼才能通過激活窗口來做到這一點?

在Excel中下面的示例代碼打開桌面上的一個PPT文件,並試圖爲這個PPT的變焦,但它似乎沒有工作:

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 

從Excel中的Excel文件設置變焦文件的工作原理與略有不同的說法(不View):

Application.ActiveWindow.Zoom = 100 

有誰知道如何在不的PPT文件的任何代碼解決這個問題?

回答

1

你的代碼運行順暢使用後期招標:

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 
+0

謝謝你的回答。但我認爲這與晚期或早期的約束無關。當我打開一個新的ppt,然後運行代碼時,它同時適用於遲到和早期綁定。看來,當我嘗試設置我的VBA創建的ppt文件的縮放時,這兩個代碼只會出現自動化錯誤。 – JackTheFruit

+0

@JackTheFruit:該應用是否可見?請修改您的帖子以提供完整的代碼,以便我們可以看到您正在做什麼。 – R3uK

+0

@JackTheFruit:我編輯了我的帖子以包含代碼的工作版本,我還用'Set ppPres = ppApp.Presentations.Add'對其進行了測試,並且這兩種情況對我來說都很好。 – R3uK

2

一些幫助(謝謝R3uK!)我發現基於問題的最後代碼如下解決方案:

解決方案:

而不是ppApp.ActiveWindow.View.Zoom = 100使用以下命令:

ppPres.Windows(1).Panes(2).Activate - >啓動主滑窗格
ppPres.Windows(1).View.Zoom = 100 - >設置活動窗格的縮放窗口


一些言論:

ppPres.Windows(1) - >收集只有窗口的演示文稿顯示在

ppApp.Windows(1) - >集合與所有打開的PPT瓦特INDOWS。因此,如果在項目1之前打開了一個ppt,則不會返回右側窗口。

項目1::ppViewThumbnails(左側微型幻燈片)
第2項: -

ppPres.Windows(1).Panes(2)> PPT的窗口的內窗格這是在我的情況下,下面ViewTypes依賴從項目號碼的集合ppViewSlide (主視圖)
項目3:ppViewNotesPage(底部註釋部分)

所以在我的例子,我想改變項目2主幻燈片視圖窗格中的變焦。

相關問題