使用 VBA 在 PowerPoint 中插入/更新图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15766931/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:21:30  来源:igfitidea点击:

Using VBA to insert/update image in PowerPoint?

imagevbapowerpoint

提问by Caleb

I am making an interactive Powerpoint presentation that has previously read a text file from my local server (XAMPP) and displayed it. I'd like to now update it so that the user can hand-write their response, and it displays that on the slideshow. I have a jQuery plugin that lets someone use a stylus and it saves the 'drawing' as an image on the local server.

我正在制作一个交互式 Powerpoint 演示文稿,该演示文稿以前从我的本地服务器 (XAMPP) 读取文本文件并显示它。我现在想更新它,以便用户可以手写他们的回复,并将其显示在幻灯片上。我有一个 jQuery 插件,可以让某人使用手写笔,并将“绘图”保存为本地服务器上的图像。

My question is, how can I insert the image into PowerPoint using a VBA Macro? Or since the image always will have the same file path, but will be replaced with a different image, can I somehow "Update" the image on the slide? Sorry if it's confusing.

我的问题是,如何使用 VBA 宏将图像插入 PowerPoint?或者由于图像始终具有相同的文件路径,但会被替换为不同的图像,我可以以某种方式“更新”幻灯片上的图像吗?对不起,如果它令人困惑。

This is the VBA I tried:

这是我试过的VBA:

Sub insert()
    Dim oPic As Shape
    Set oPic = ActivePresentation.Slides(1).Shapes.AddPicture("http://localhost/image.png", False, True, 0, 0, -1, -1)
End Sub

I could also get the path of the image on the computer, not the server.

我还可以在计算机上而不是服务器上获取图像的路径。

When I run the macro, I get a "file not found" error. Does anyone know what's wrong?

当我运行宏时,出现“找不到文件”错误。有谁知道出了什么问题?

Thanks!

谢谢!

PS: If anyone's interested, the plugin is called Signature Pad.

PS:如果有人感兴趣,该插件称为Signature Pad

回答by Caleb

Looks like I can insert an image if it's in the exact same folder:

看起来我可以插入一个图像,如果它在完全相同的文件夹中:

Sub insert()
    Dim oPic As Shape
    Set oPic = ActivePresentation.Slides(1).Shapes.AddPicture("image.png", False, True, 0, 0, -1, -1)
End Sub

This works, but if anyone knows how to go into another folder, please let me know. If I try folder/image.pngor /folder/image.pngas the file path, it doesn't work.

这有效,但如果有人知道如何进入另一个文件夹,请告诉我。如果我尝试folder/image.png/folder/image.png作为文件路径,它不起作用。

回答by Rob

It doesn't need to be in the exact same folder, but you do need to use Mac-style paths:

它不需要位于完全相同的文件夹中,但您确实需要使用 Mac 样式的路径:

#If Mac Then
    imagePath = (MacScript("get path to startup disk as string") & "Users:name:Desktop:SomeFolder:My_Picture.png")
#Else
    imagePath = "C:\path\to\My_Picture.png"
#End If

activeSlide.Shapes.AddPicture(imagePath, False, True, 10, 10)

Hope that helps.

希望有帮助。