PPT宏代码

以下代码适用于:当每张幻灯片由一张图制作,快速统一图片格式。

1、将所有ppt中所有幻灯片中的图更改为宽度34cm,固定纵横比

vbscript 复制代码
Sub ResizeAllPictures()
    Dim sld As Slide
    Dim shp As Shape
    Dim targetWidth As Single
    Dim aspectRatio As Single
    
    ' 设置目标宽度为34厘米
    ' 1厘米 = 28.34646磅 (Points),这是PowerPoint内部单位
    targetWidth = 34 * 28.34646
    
    ' 遍历所有幻灯片
    For Each sld In ActivePresentation.Slides
        ' 遍历幻灯片中的所有形状
        For Each shp In sld.Shapes
            ' 判断形状是否为图片
            If shp.Type = msoPicture Then
                ' 计算当前图片的宽高比
                aspectRatio = shp.Height / shp.Width
                
                ' 设置宽度为34厘米(转换为磅值)
                shp.Width = targetWidth
                
                ' 根据原始宽高比设置高度
                shp.Height = targetWidth * aspectRatio
            End If
        Next shp
    Next sld
    
    MsgBox "已将所有图片宽度设置为34厘米,并保持纵横比!", vbInformation, "操作完成"
End Sub

2、将ppt所有幻灯片中的图片使用代码一键上下居中、左右居中

vbscript 复制代码
Sub CenterAllPicturesEnhanced()
    Dim sld As Slide
    Dim shp As Shape
    Dim slideWidth As Single
    Dim slideHeight As Single
    Dim picCount As Integer
    Dim originalAspectRatio As Boolean
    
    On Error GoTo ErrorHandler
    
    picCount = 0
    
    ' 遍历所有幻灯片
    For Each sld In ActivePresentation.Slides
        slideWidth = sld.Master.Width
        slideHeight = sld.Master.Height
        
        ' 遍历幻灯片中的所有形状
        For Each shp In sld.Shapes
            ' 检查形状是否为图片且可见
            If shp.Type = msoPicture And shp.Visible Then
                ' 保存原始纵横比设置
                originalAspectRatio = shp.LockAspectRatio
                ' 确保纵横比锁定,防止图片变形
                shp.LockAspectRatio = msoTrue
                
                ' 计算居中位置:cite[1]
                shp.Left = (slideWidth - shp.Width) / 2
                shp.Top = (slideHeight - shp.Height) / 2
                
                ' 恢复原始纵横比设置
                shp.LockAspectRatio = originalAspectRatio
                
                picCount = picCount + 1
            End If
        Next shp
    Next sld
    
    If picCount > 0 Then
        MsgBox "成功将 " & picCount & " 张图片在各自幻灯片中居中对齐。", vbInformation, "操作完成"
    Else
        MsgBox "未在演示文稿中找到任何图片。", vbExclamation, "提示"
    End If
    
    Exit Sub
    
ErrorHandler:
    MsgBox "发生错误:" & Err.Description, vbCritical, "错误"
End Sub
相关推荐
dingdingfish14 分钟前
TLPI 第49章 读书笔记:Memory Mapping
linux·mmap·munmap·tlpi·msync
电科_银尘25 分钟前
【深度学习项目实战·04】VS Code远程开发:在本地编辑,在服务器运行与调试
服务器·人工智能·深度学习
EVEN-091327 分钟前
Linux-进程(2)
linux·运维·服务器
u01090535931 分钟前
(永久免费)树莓派 / 香橙派等开发板,安装神卓互联巴比达内网穿透安装教程(Linux ARM 版 V9.4.1)
linux·运维·arm开发
Linux_cy32 分钟前
服务器管理方案选型:面板 / SSH 客户端 / 桌面型客户端怎么选
java·linux·运维·服务器·ssh·可视化·gmssh
熊猫钓鱼>_>1 小时前
CMP for OpenHarmony 实战:Coil 图片加载库鸿蒙化使用心得
运维·服务器·开发语言·华为·ai编程·harmonyos·openharmony
可乐_不渴1 小时前
【Linux】 进程虚拟地址空间
java·linux·javascript
byte轻骑兵1 小时前
hcitool 工具:HCI 层指令实操 + 源码中指令的封装逻辑
linux·aiot·bluez·电脑蓝牙·嵌入式蓝牙
qeen871 小时前
【Linux】磁盘读写文件原理及文件系统基本概念介绍
linux·运维·服务器·笔记·学习
ITxiaobing20231 小时前
海外广告投放:AdTech 场景下 IP 地址库选型思考
服务器·网络·tcp/ip