我在Excel中構建了一個VBA宏,用於將Excel範圍和Excel圖形複製到PowerPoint中。爲此,我想打開一個現有的演示文稿(pptName)。Excel to PowerPoint - 如果ppt打開但特定的pres不打開,則打開特定的pres,否則使用已打開的pres
我很可能已經打開了演示文稿,還有其他演示文稿的集合。
我想要的代碼: 查找PowerPoint是否打開;如果它打開,然後檢查pptName。如果pptName已經打開,則使用腳本繼續,否則打開pptName。
問題: 我似乎無法讓它使用已經打開的pptName。要麼打開演示文稿的第二個新實例,要麼使用最近使用的演示文稿,這通常不是我希望編輯的特定演示文稿。
代碼: 昏暗ppApp作爲PowerPoint.Application 昏暗ppSlide作爲PowerPoint.Slide
Dim pptName As String
Dim CurrentlyOpenPresentation As Presentation
pptName = "MonthlyPerformanceReport"
'Look for existing instance
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application
'Add a presentation if none exists
'If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add
'If ppt is open, check for pptName. If pptName is already open then progress, otherwise open pptName
If ppApp.Presentations.Count > 0 Then
For Each CurrentlyOpenPresentation In ppApp.Presentations
If CurrentlyOpenPresentation.FullName = pptName & ".pptx" Then GoTo ProgressWithScript
Next CurrentlyOpenPresentation
ppApp.Presentations.Open Filename:=SheetLocation & "\" & pptName & ".pptx"
End If
ProgressWithScript:
'Open Presentation specified by pptName variable
If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Open Filename:=SheetLocation & "\" & pptName & ".pptx"
'If ppApp.Presentations.Count > 0 Then ppApp.Presentations.Open Filename:=SheetLocation & "\" & pptName & ".pptx"
'Application.DisplayAlerts = False
另一種嘗試,仍然沒有正確的:
If ppApp.Presentations.Count > 0 _
Then
For Each CurrentlyOpenPresentation In ppApp.Presentations
If CurrentlyOpenPresentation.FullName = pptName _
Then IsOpen = True
If CurrentlyOpenPresentation.FullName = pptName _
Then ppApp.ActiveWindow.View.GotoSlide ppApp.Presentations(pptName).Slides.Count
If IsOpen = True Then GoTo ProgressWithScript
Next CurrentlyOpenPresentation
'Else: ppApp.Presentations.Open Filename:=SheetLocation & "\" & pptName & ".pptm"
End If
IsOpen = False
If IsOpen = False _
Then ppApp.Presentations.Open Filename:=SheetLocation & "\" & pptName & ".pptm"
基本的問題是,你遍歷集合演示看看是否匹配.FullName的SomeFileName.PPTX演示,它永遠不會因爲.FullName返回完整路徑,而不僅僅是文件名。 .Name,正如你發現的那樣,返回名字(包括擴展名,所以不需要將它粘貼到你比較的文件名上)。 – 2014-09-28 16:19:43