vba PPT中可以使用VBA代码从剪贴板粘贴到PNG吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12675927/
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
Can VBA code be used in PPT to paste from clipboard to PNG?
提问by BrittanyRuth
I've tried numerous versions to get this code. This one at least errors out... I want to be in PowerPoint, Paste Special from the Clipboard to PNG.
我已经尝试了很多版本来获取此代码。这个至少出错了......我想在PowerPoint中,从剪贴板粘贴特殊到PNG。
Sub Test()
ActivePresentation.Slides.Paste.Shapes.PasteSpecial ppPastePNG
End Sub
This errors out to say
这个错误说
"Invalid Request. Clipboard is empty or contains data which may not be pasted here."
“无效请求。剪贴板为空或包含可能无法粘贴到此处的数据。”
The clipboard is not empty and does contain data I can paste in.
剪贴板不是空的,并且包含我可以粘贴的数据。
Thanks for any ideas!
感谢您的任何想法!
回答by BrittanyRuth
FYI - after much trial and error, I came up with this macro. It copies the slide content and pastes it back to the slide as PNG.
仅供参考 - 经过多次反复试验,我想出了这个宏。它复制幻灯片内容并将其作为 PNG 粘贴回幻灯片。
Sub test()
Dim sld As Slide
Dim pre As Presentation
Dim shp1 As Shape
Dim shp2 As Shape
Set pre = ActivePresentation
For Each sld In pre.Slides
Set shp1 = sld.Shapes.AddShape(msoShapeRectangle, 0, 0, 20, 20)
shp1.Line.Visible = msoFalse
shp1.Fill.Transparency = 1
Set shp2 = sld.Shapes.AddShape(msoShapeRectangle, 0, 0, 20, 20)
shp2.Left = 720 - shp2.Width
shp2.Top = 540 - shp2.Width
shp2.Line.Visible = msoFalse
shp2.Fill.Transparency = 1
Next
For Each sld In pre.Slides
sld.Shapes.Range.Cut
If sld.Shapes.Count > 0 Then
sld.Shapes.Range.Delete
sld.Shapes.PasteSpecial ppPastePNG
sld.Shapes.Range.Align msoAlignCenters, msoTrue
sld.Shapes.Range.Align msoAlignMiddles, msoTrue
End If
If sld.Shapes.Count = 0 Then
sld.Shapes.PasteSpecial ppPastePNG
sld.Shapes.Range.Align msoAlignCenters, msoTrue
sld.Shapes.Range.Align msoAlignMiddles, msoTrue
End If
Next
End Sub
回答by cdarlint
Simple answer:
简单回答:
Set m = ActivePresentation
n = m.Slides.Count
m.Slides.Add n + 1, ppLayoutBlank
m.Slides(2).Shapes.PasteSpecial ppPastePng
reference page on msdn for powerpoint:
"shape" paste formats:
http://msdn.microsoft.com/en-us/library/office/ff745158(v=office.15).aspx
msdn 上的 powerpoint 参考页面:
“形状”粘贴格式:http: //msdn.microsoft.com/en-us/library/office/ff745158(v=office.15)
.aspx
new slide layouts:
http://msdn.microsoft.com/en-us/library/office/ff745137(v=office.15).aspx
新幻灯片布局:http: //msdn.microsoft.com/en-us/library/office/ff745137(v=
office.15).aspx
Alternatively if plain text is desired:
或者,如果需要纯文本:
m.Slides.Add n + 1, ppLayoutText
m.Slides(n + 1).Shapes(2).TextFrame.TextRange.PasteSpecial (ppPasteText)
"textrange" pastespecial formats:
http://msdn.microsoft.com/en-us/library/office/ff745706(v=office.15).aspx
“文本范围”粘贴特殊格式:http: //msdn.microsoft.com/en-us/library/office/ff745706(v=office.15)
.aspx
Or even get text into variable:
或者甚至将文本放入变量中:
myVar = m.Slides(1).Shapes(1).TextFrame.TextRange.Text
"textrange" text property:
http://msdn.microsoft.com/en-us/library/office/ff746239(v=office.15).aspx
“textrange”文本属性:http: //msdn.microsoft.com/en-us/library/office/ff746239(v=office.15)
.aspx
P.S.
Lowercase is also acceptable for properties or methods, although they are all CAPITAL letters in documentation.
Brackets on function calling may be omitted as above first code block.
PS
小写也可以用于属性或方法,尽管它们在文档中都是大写字母。
函数调用上的括号可以省略,如上面的第一个代码块。