搜索具有指定标签的幻灯片并将其替换为主演示文稿中的幻灯片

Posted

技术标签:

【中文标题】搜索具有指定标签的幻灯片并将其替换为主演示文稿中的幻灯片【英文标题】:Searching for a slide with a specified tag and replacing it with a slide from a master presentation 【发布时间】:2021-09-03 08:24:33 【问题描述】:

我希望在演示文稿中搜索一张或多张具有指定标签的幻灯片。找到后,我想用母版演示文稿中的另一张幻灯片替换该幻灯片。

我试图用我收集的其他 VBA 的一部分创建一个解决方案。我感觉我已经很接近了,但还没有到那里(注意下面的内容让我陷入了循环)。

任何帮助将不胜感激

Sub ReplaceSlideThatHasTag()


For Each osld In ActivePresentation.Slides

'here I am selecting the slide that has the tag name "winter" and the tag id "123
If osld.Tags("WINTER") = "123" Then osld.Select

'here I am trying to add slide 27 from my master presentation immediately before the slide with the tag
ActivePresentation.Slides.InsertFromFile ("C:\my files\master presentation.PPTX"), ActiveWindow.Selection.SlideRange.SlideIndex, 24, 24

'and finally I am looking to delete the slide with the tag
If osld.Tags("WINTER") = "123" Then osld.Delete


Next osld

End Sub

【问题讨论】:

【参考方案1】:

一些一般性的建议:除非无法避免,否则永远不要选择任何东西,而且几乎从来没有无法避免的情况。下面的建议(带评论)。试试这个版本。

Sub ReplaceSlideThatHasTag()

' ALWAYS dim your variables before using them
Dim osld as Slide

For Each osld In ActivePresentation.Slides

'here I am selecting the slide that has the tag name "winter" and the tag id "123

If osld.Tags("WINTER") = "123" Then '  DON'T select anythin osld.Select

'here I am trying to add slide 27 from my master presentation immediately before the slide with the tag
'ActivePresentation.Slides.InsertFromFile ("C:\my files\master presentation.PPTX"), 'ActiveWindow.Selection.SlideRange.SlideIndex, 24, 24

' But since we're not selecting anything ...
ActivePresentation.Slides.InsertFromFile ("C:\my files\master presentation.PPTX"), _ osld.SlideIndex

'and finally I am looking to delete the slide with the tag
' But we already have a reference to the slide in the osld variable
' and we know that the slide has the tag so ...
'If osld.Tags("WINTER") = "123" Then osld.Delete
osld.Delete

' And since we've found the slide and done the deed,
' no need to continue...
Exit For
End If
Next osld

End Sub

【讨论】:

非常感谢您的建议 - 这真的很有用。我无法让您的代码正常工作 - 我收到“没有 for 的下一个”错误。任何想法我做错了什么? 抱歉,我的错。我已经编辑了代码来解决这个问题。只需在 EXIT FOR 后添加一个 END IF

以上是关于搜索具有指定标签的幻灯片并将其替换为主演示文稿中的幻灯片的主要内容,如果未能解决你的问题,请参考以下文章

使用 Java 将 OpenOffice 幻灯片从一个演示文稿复制到另一个演示文稿

text 如何仅删除演示文稿中的某些幻灯片?

text 如何从保存在多个文件中的幻灯片快速编译单个PowerPoint演示文稿?

Excel 到 PowerPoint VBA

电脑ppt保存不了,怎么回事?

Xaringan 演示文稿的演示者模式不同步