我有旧代码,我将其重新用于更广泛的用途。
我有一个 PowerPoint 演示文稿,我想将特定的图像文件粘贴到其中,创建一个新幻灯片,然后重复直到 A 列中的所有变量名称都完成。
它在特定文件位置查找图像名称,根据变量名称值的左侧、变量名称值(A 列)和变量名称值的右侧构建名称。前任。(“设备”“23”“通用产品线”)。
找到此图像名称后,它将该图像插入幻灯片,调整大小并将其定位到左侧,然后找到另一个比较图像,将其放置在同一张幻灯片上并将其调整大小并将其定位到右侧。
调整大小和定位不再正常工作。似乎图像没有被视为形状。我认为第一张图像是之前实验中的形状(2),因为幻灯片上有一些剪贴画可以算作形状。出于同样的原因,我然后让形状(3)是图像 2。
Sub Export_To_PowerPoint_JAH()
' Keyboard Shortcut: Ctrl+Shift+M
Dim Shape1 As PowerPoint.Shape
Dim Shape2 As PowerPoint.Shape
Dim objSlide As Slide
Dim New_Slide As Slide
Dim pptLayout As CustomLayout
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
'Create a PP application and make it visible
Set PP = New PowerPoint.Application
PP.Visible = msoCTrue
'Open the presentation you wish to copy to
'Opens the Template
Set PPpres = PP.Presentations.Open("A file path name to a template")
i = 7
Pre_Left = Range("H2")
Pre_Right = Range("H4")
Post_Left = Range("K2")
Post_Right2 = Range("K4")
Do
Set objSlide = PPpres.Slides(i - 5)
Set Title = PPpres.Slides(i - 5)
If Cells(i, 1) = "" Then
Exit Do
Else: End If
Variable_Name = Cells(i, 1)
'Searches Image Bank Folder for pre and post file names
If Not Range("H2") = "" Then
Image_Name_Pre = Pre_Left & " " & Variable_Name & " " & Pre_Right
Else
Image_Name_Pre = Variable_Name & " " & Pre_Right
End If
If Not Range("K2") = "" Then
Image_Name_Post = Post_Left & " " & Variable_Name & " " & Post_Right2
Else
Image_Name_Post = Variable_Name & " " & Post_Right2
End If
Set Shape1 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Pre, msoCTrue, msoCTrue, 100, 100)
objSlide.Shapes.Item(2).Width = 300
objSlide.Shapes.Item(2).Height = 400
objSlide.Shapes.Item(2).Top = 140
objSlide.Shapes.Item(2).Left = 90
Set Shape2 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Post, msoCTrue, msoCTrue, 100, 100)
objSlide.Shapes.Item(3).Width = 300
objSlide.Shapes.Item(3).Height = 400
objSlide.Shapes.Item(3).Top = 140
objSlide.Shapes.Item(3).Left = 500
Title.Shapes.Title.TextFrame.TextRange.Text = Cells(i, 3) & " Pre (Left) : " & Cells(i, 3) & " Post (Right) Offset=" & Cells(i, 4)
'Create new slide
Set New_Slide = PPpres.Slides.Add(PPpres.Slides.Count + 1, PpSlideLayout.ppLayoutObject)
'ActivePresentation.Slides.Add Index:=ActivePresentation.Slides.Count + 1, Layout:=ppLayoutCustom
i = i + 1
Loop
End Sub
假设您想要的形状将是幻灯片上的第 n 个形状并不是一个好主意,在您的情况下,没有必要这样做。这:
Set Shape1 = objSlide.Shapes.AddPicture(Range("B5") & Image_Name_Pre, msoCTrue, msoCTrue, 100, 100)
为您提供对变量 Shape1 中新插入图像的引用,因此您可以执行以下操作:
With Shape1
.Width = 300
.Height = 400
.Top = 140
.Left = 90
End With
Shape2 也是如此。
另外,你这样做:
Set Title = PPpres.Slides(i - 5)
这里有两个问题:
1) 你还没有声明变量 Title,并且
2) 使用对象/方法/属性名称作为变量名称不是一个好习惯。
反而:
Dim oTitle as Slide
Set oTitle = PPpres.Slides(i - 5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句