如何编写用于按幻灯片标题插入图像的 VBA 代码 [英] How to Write VBA Code for inserting images as per slide title

查看:52
本文介绍了如何编写用于按幻灯片标题插入图像的 VBA 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写 VBA 代码,用于根据文件夹中的幻灯片名称插入图像,这意味着在运行 VBA 后,它会根据幻灯片名称自动插入图像

I want to write VBA code for inserting the image as per the slide name from the folder means after running the VBA it automatically inserts the images as per the slide name

例如:如果幻灯片包含顶视图"在文本框中,然后通过运行 VBA 脚本,它应该自动选择名称为顶视图"的图片.来自特定文件夹.

For eg: if the slide contains "Top View" in the text box then by running the VBA script it should automatically pick the picture having name "Top View" from the particular folder.

我已经尝试过这段代码,但它似乎无法创建这种关系

I have tried this code but it seems its unable to create that relations

Sub image_insert() 

    Dim objPresentaion As Presentation
    Dim objSlide As Slide
    Dim objImageBox As Shape 
    Set objPresentaion = ActivePresentation
    Set objSlide = objPresentaion.Slides.Item(1)
    Set objImageBox = objSlide.Shapes.AddPicture("C:\Users\mehta\Desktop\Folder for ppt images\Top_View.jpg", msoCTrue, msoCTrue, 25, 25) 
End Sub

推荐答案

试试这个.我添加了一些错误测试和一个返回幻灯片标题文本的基本函数;只有当您要查找的文本确实是幻灯片标题(即标题占位符中的文本)而不是一些格式化为看起来像标题的随机文本时,这才有效.

Give this a try. I've added some error tests and a basic function to return the slide title text; that'll only work if the text you're after truly IS a slide title (ie text in a title placeholder) and not some random text formatted to look like a title.

我没有时间设置一个包含测试图像和幻灯片的文件夹以匹配它们的名称,但请尝试一下……如果不完全正确,它应该很接近.

I haven't the time to set up a folder of test images and slides to match their names, but give it a try ... it should be close, if not exactly right.

Option Explicit

Sub image_insert()

    Dim objPresentaion As Presentation
    Dim objSlide As Slide
    Dim objImageBox As Shape
    Dim sSlideTitle As String
    Dim sFolder As String
    
    Set objPresentaion = ActivePresentation
    sFolder = "C:\Users\mehta\Desktop\Folder for ppt images\Top_View.jpg"

    
    For Each objSlide In objPresentaion.Slides
        sSlideTitle = GetTitleText(objSlide)
        ' WAS there a title on the slide?
        If Len(sSlideTitle) > 0 Then
            ' make sure the image exists
            If Len(Dir$(sFolder & sSlideTitle & ".JPG")) > 0 Then
                Set objImageBox = objSlide.Shapes.AddPicture(sFolder & sSlideTitle & ".JPG", _
                msoCTrue, msoCTrue, 25, 25)
            Else
               ' Comment this out later
               ' MsgBox "Image missing: " & sSlideTitle
            End If
        Else
            ' comment this out later:
            MsgBox "This slide has no title"
        End If
  
    Next    ' Slide

End Sub

Function GetTitleText(oSl As Slide) As String
    
    Dim sTemp As String
    
    With oSl
        ' handle errors in case there's no slide title
        On Error Resume Next
        sTemp = .Shapes.Title.TextFrame.TextRange.Text
        If Err.Number <> 0 Then
            sTemp = ""
        End If
    End With
    
    GetTitleText = sTemp
    
End Function

这篇关于如何编写用于按幻灯片标题插入图像的 VBA 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆