2017-08-08 177 views
0

我試圖在powerpoint中創建一個宏來自動執行一些常規任務。所以,我有一個有60多張幻燈片的大師幻燈片。我想創建一個宏,它會遍歷整個卡組並複製其中包含特定文本的特定幻燈片。我可以使用構成選擇基礎的關鍵詞創建數組,但無法弄清楚如何複製整個幻燈片。下面的代碼是在互聯網上覓食的結果。VBA根據特定詞選擇幻燈片並複製到新演示文稿

Sub selct() 

Dim pres1 As PowerPoint.Presentation, pres2 As PowerPoint.Presentation, 
pp As Object 
Set pp = GetObject(, "PowerPoint.Application") 

Set pres1 = pp.ActivePresentation 
Set pres2 = pp.Presentations.Add 

Dim i As Long, n As Long 
Dim TargetList 

'~~> Array of terms to search for 
TargetList = Array("Agenda", "Review", "third", "etc") 

'~~> Loop through each slide 
For Each sld In pres1.Slides 
    '~~> Loop through each shape 
    For Each shp In sld.Shapes 
     '~~> Check if it has text 
     If shp.HasTextFrame Then 
      Set txtRng = shp.TextFrame.TextRange 

      For i = 0 To UBound(TargetList) 
       '~~> Find the text 
       Set rngFound = txtRng.Find(TargetList(i)) 

       '~~~> If found 
       Do While Not rngFound Is Nothing 
        '~~> Set the marker so that the next find starts from here 
        n = rngFound.Start + 1 
        '~~> Chnage attributes 
        With rngFound.Font 
         .Bold = msoFalse 
         sld.Copy 
         pres2.Slides.Paste 
         '~~> Find Next instance 
         Set rngFound = txtRng.Find(TargetList(i), n) 
        End With 
       Loop 
      Next 
     End If 
    Next 
Next 
End Sub 

以上副本的幻燈片,但沒有格式。此外,幻燈片會重複出現,以便主演示文稿中新幻燈片的數量(當它應該是子集)。例如,主人有60張幻燈片,新的幻燈片也有60張幻燈片,而不是20張。

如何複製與目標數組中的特定詞相同的幻燈片,並保留幻燈片的格式?

任何幫助將不勝感激!

感謝

小號

+0

查看PowerPoint對象模型參考文獻,特別是'Slide Object'類型,並注意它是'Copy'方法。 https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/slide-object-powerpoint還有一個'Select'方法,但它可能沒有必要使用它,你可以只是'複製'並直接粘貼。 –

+0

感謝David,我使用了Copy方法並修改了代碼,如下所示 – shree

+0

我使用了.Copy函數並且它可以工作。但是粘貼的幻燈片是重複的,因此新演示文稿中幻燈片的總數爲主,並且沒有源格式。任何想法如何我只能複製特定的幻燈片,並保持源格式。謝謝 – shree

回答

0

我覺得首先你需要確保pres2使用相同的設計模板/主題爲pres1。如果pres2正在使用不同的主題,那麼幻燈片將反映該主題。從pres2

首先,刪除所有幻燈片:我不記得怎麼做,而不需要花費一定的時間調試它,但因爲你是從一個空白演示文稿開始,大概這是最簡單的

Set pres2 = pp.Presentations.Add 
Dim i as Long 
For i = pres2.Slides.Count to 1 Step - 1 
    pres2.Slides(i).Delete 
Next 

現在你有一個空的演示文稿,Paste來自pres1的幻燈片應該保留佈局/主題。

sld.Copy 
pres2.Slides.Paste 
相關問題