2017-08-08 49 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 
相关问题