使用 vba 在 PowerPoint 2007 中的幻灯片上定位图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26677302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Positioning images on slides in PowerPoint 2007 using vba
提问by jedmatic
I'd like to do one of two things in PowerPoint 2007 on Windows.
我想在 Windows 上的 PowerPoint 2007 中执行以下两项操作之一。
The first is to change the default location for pasting in an image. When I paste in a graph I made with SAS, it pastes into the upper lefthand corner. Ideally, I'd like to change the default paste position. There don't seem to be any simple options for this but I thought maybe it was possible with VBA.
第一个是更改粘贴图像的默认位置。当我粘贴用 SAS 制作的图形时,它会粘贴到左上角。理想情况下,我想更改默认粘贴位置。似乎没有任何简单的选择,但我认为 VBA 可能是可能的。
If it's not possible, then I'd like to write a VBA macro to step through each slide and change the image position.
如果不可能,那么我想编写一个 VBA 宏来逐步浏览每张幻灯片并更改图像位置。
I got a slide loop to work, thanks to this and other sites (the MsgBox is just a test):
多亏了这个网站和其他网站(MsgBox 只是一个测试),我得到了一个幻灯片循环:
Sub SlideLoop()
Dim osld As Slide
For Each osld In ActivePresentation.Slides
osld.Select
MsgBox "The slide index of the current slide is: " & _
ActiveWindow.View.Slide.SlideIndex
Next osld
End Sub
Beyond that, I haven't had much luck. I have seen code snippets that select all images on a slide and crop or resize them, and I found this bit on excelhelphq.com that is meant to position an image:
除此之外,我没有太多运气。我见过选择幻灯片上的所有图像并裁剪或调整它们大小的代码片段,我在 excelhelphq.com 上发现了这一点,用于定位图像:
With ActiveWindow.Selection.ShapeRange
.Left = 50 'change the number for desired x position
.Top = 50 'change the number for desired y position
End With
But I'm not sure how to integrate it into the loop, and the online documentation for Powerpoint VBA is not particularly robust. Some code deals with the ShapeIndex but I wasn't sure how to work with that.
但是我不确定如何将它集成到循环中,而且 Powerpoint VBA 的在线文档也不是特别可靠。一些代码处理 ShapeIndex 但我不确定如何处理它。
I should mention that I have only one image on a slide when I have an image (some slides do not have images at all, though).
我应该提到的是,当我有图像时,幻灯片上只有一个图像(不过,有些幻灯片根本没有图像)。
This seems like the best time-saving approach, though I'm still manually pasting into PowerPoint in the first place.
这似乎是最好的节省时间的方法,尽管我仍然首先手动粘贴到 PowerPoint 中。
I appreciate any help with this! I couldn't find anything that addressed this exact question.
我感谢任何帮助!我找不到任何可以解决这个确切问题的内容。
Is VBA for PPT being phased out? It feels like Microsoft doesn't want people to be able to figure out how to use it based on their not-stellar online documentation.
PPT 的 VBA 会被淘汰吗?感觉微软不希望人们能够根据他们不太出色的在线文档弄清楚如何使用它。
回答by Steve Rindsberg
It's unlikely that MS will phase out VBA any time soon. Too many large corporate clients would roast them if they did. All the same, you're right, the documentation is bad and getting worse with every release.
MS 不太可能很快淘汰 VBA。如果他们这样做,太多的大型企业客户会烤它们。尽管如此,您是对的,文档很糟糕,并且随着每个版本的发布而变得更糟。
Makes places like this all the more valuable, then. So, a little cleanup to your basic loop first:
那么,让这样的地方更有价值。因此,首先对您的基本循环进行一些清理:
Sub SlideLoop()
Dim osld As Slide
For Each osld In ActivePresentation.Slides
' osld.Select No need to select a slide before acting on it
'MsgBox "The slide index of the current slide is: " & _
' ActiveWindow.View.Slide.SlideIndex
MsgBox "The slide index of the current slide is: " & cstr(osld.SlideIndex)
Next osld
End Sub
So here's a start on how to do what you're after:
所以这是一个关于如何做你所追求的事情的开始:
Sub SlideLoop()
Dim osld As Slide
Dim oSh As Shape
For Each osld In ActivePresentation.Slides
' check each shape on the slide
' is it an image or whatever you're looking for?
For Each oSh In osld.Shapes
With oSh
If .Type = msoLinkedPicture _
Or .Type = msoPicture Then
' position it to taste
.Left = 100
.Top = 100
' centering/resizing gets trickier
' but is still possible.
' Exercise for the reader?
' Hint:
' ActivePresentation.PageSetup.SlideWidth and .SlideHeight
' tells you the width and height of the slide
'
' All values are in Points (72 to the inch)
End If
End With
Next ' Shape
Next osld ' Slide
End Sub