[英]Trouble with VBA Do While loop in macro to copy slide and change picture in powerpoint
I am attempting my first macro in powerpoint. 我正在尝试在Powerpoint中创建第一个宏。 I am simply trying to copy a 'reference' slide and replace one picture. 我只是试图复制一张“参考”幻灯片并替换一张图片。 I will do this for all pictures in a folder. 我将对文件夹中的所有图片执行此操作。 The script I have adds all the slides, but then adds all the pictures to the final slide. 我拥有的脚本添加了所有幻灯片,但随后将所有图片添加到了最终的幻灯片中。 I don't understand why this is the case since the Duplicate Slide code and the Add Picture code are in the same while loop. 我不明白为什么会这样,因为重复幻灯片代码和添加图片代码在同一while循环中。 Can someone tell me why? 有人可以告诉我为什么吗?
Sub LoopThroughFiles()
Dim StrFile As String
Dim Folder As String
Dim sld As Slide
Dim referencesld As Slide
Set referencesld = ActivePresentation.Slides(1)
Dim i As Integer
Dim shp As Shape
i = 1
Folder = "c:\E1B8\ScriptTesting\MISC\PPT\SampleData2\"
StrFile = Dir(Folder & "*")
Set referencesld = ActivePresentation.Slides(1)
Set shp = referencesld.Shapes(5)
Do While Len(StrFile) > 0
Debug.Print Folder & StrFile
referencesld.Duplicate
i = i + 1
Set sld = ActivePresentation.Slides(i)
With shp
sld.Shapes.AddPicture(Folder & StrFile, msoFalse, msoTrue, .Left, .Top, .Width, .Height).IncrementRotation 360#
End With
sld.Shapes(5).Delete
StrFile = Dir
Loop
End Sub
Thanks!! 谢谢!!
You can use a variable when duplicating the slide, forget the "i" variable, it is not needed: 您可以在复制幻灯片时使用变量,而不必使用“ i”变量,因此不需要:
delete the lines 删除行
referencesld.Duplicate
i = i + 1
Set sld = ActivePresentation.Slides(i)
An insert "Set sld = referencesld.Duplicate" in the place 在该位置插入“ Set sld = referencesld.Duplicate”
...
Debug.Print Folder & StrFile
Set sld = referencesld.Duplicate
With shp
sld.Shapes.AddPicture(Folder & StrFile, msoFalse, msoTrue, .Left, .Top, .Width, .Height).IncrementRotation 360#
End With
...
Untested 未经测试
You can see "Duplicate" reference in http://msdn.microsoft.com/library/office/ff745804(v=office.15).aspx 您可以在http://msdn.microsoft.com/library/office/ff745804(v=office.15).aspx中看到“重复”参考
Aside: I think it is weird to use the "with" keyword on the argument (shape) instead of the object you are working on (the slide). 另外:我认为在参数(形状)上使用“ with”关键字而不是要处理的对象(幻灯片)很奇怪。
[edited: Make it clear to understand what to do] [编辑:明确说明该怎么做]
I hope to help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.