|
|

楼主 |
发表于 2025-12-8 15:59
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub InsertPicsToOpenedPPT()
' 1. 显式定义常量(兼容低版本Office,避免未定义错误)
Const msoFalse As Integer = 0
Const msoTrue As Integer = 1
Const ppLayoutBlank As Integer = 12
' 2. 定义变量(增加对象校验标记)
Dim pptApp As Object ' PowerPoint应用对象
Dim pptPres As Object ' 目标演示文稿对象
Dim pptSlide As Object ' 幻灯片对象
Dim picShape As Object ' 插入的图片对象
Dim fso As Object ' 文件系统对象(用于处理路径和文件)
Dim picFolder As String ' 图片文件夹路径
Dim targetPPTPath As String ' 目标PPT完整路径
Dim picFiles As Collection ' 存储图片路径的集合
Dim file As Object ' 单个文件对象
Dim i As Integer ' 循环变量
Dim slideWidth As Single ' 幻灯片宽度(用于自适应)
Dim slideHeight As Single ' 幻灯片高度(用于自适应)
Dim isPPTFound As Boolean ' 目标PPT是否找到的标记
' 3. 基础路径设置(根据需求修改,确保与实际文件匹配)
picFolder = "C:\9966\"
targetPPTPath = "C:\340班期中家长会.ppt" ' 注意后缀是.ppt
isPPTFound = False
' 4. 初始化文件系统对象(处理图片路径和筛选)
Set fso = CreateObject("Scripting.FileSystemObject")
Set picFiles = New Collection
' 5. 校验图片文件夹是否存在
If Not fso.FolderExists(picFolder) Then
MsgBox "错误:图片文件夹不存在!" & vbCrLf & "路径:" & picFolder, vbCritical, "路径错误"
Set fso = Nothing
Set picFiles = Nothing
Exit Sub
End If
' 6. 筛选文件夹中的图片(支持JPG/PNG/BMP/GIF主流格式)
On Error Resume Next
For Each file In fso.GetFolder(picFolder).Files
Select Case LCase(fso.GetExtensionName(file.Path))
Case "jpg", "jpeg", "png", "bmp", "gif"
picFiles.Add file.Path ' 符合格式的图片路径加入集合
End Select
Next file
On Error GoTo 0
' 7. 校验是否有可用图片
If picFiles.Count = 0 Then
MsgBox "提示:图片文件夹中未找到支持的图片(JPG/PNG/BMP/GIF)!" & vbCrLf & "路径:" & picFolder, vbExclamation, "无图片文
件"
Set file = Nothing
Set fso = Nothing
Set picFiles = Nothing
Exit Sub
End If
' 8. 按文件名升序排序图片(确保插入顺序正确)
SortPicCollection picFiles, fso
' 9. 定位已打开的PowerPoint及目标PPT
On Error Resume Next
' 9.1 检查PowerPoint是否已启动
Set pptApp = GetObject(, "PowerPoint.Application")
If pptApp Is Nothing Then
MsgBox "错误:未检测到已打开的PowerPoint程序!" & vbCrLf & "请先打开 " & targetPPTPath, vbCritical, "PPT未启动"
Set file = Nothing
Set fso = Nothing
Set picFiles = Nothing
Exit Sub
End If
pptApp.Visible = True ' 确保PPT窗口可见,避免后台运行导致操作异常
' 9.2 查找已打开的目标PPT(通过完整路径匹配,避免同名文件混淆)
For Each pptPres In pptApp.Presentations
If fso.GetAbsolutePathName(pptPres.FullName) = fso.GetAbsolutePathName(targetPPTPath) Then
isPPTFound = True
Exit For
End If
Next pptPres
On Error GoTo 0
' 9.3 校验目标PPT是否已打开
If Not isPPTFound Then
MsgBox "错误:目标PPT未打开!" & vbCrLf & "请先打开:" & targetPPTPath, vbCritical, "PPT未找到"
Set file = Nothing
Set fso = Nothing
Set picFiles = Nothing
Set pptApp = Nothing
Exit Sub
End If
' 10. 获取幻灯片尺寸(用于图片自适应,支持不同幻灯片比例)
On Error Resume Next
slideWidth = pptPres.PageSetup.slideWidth
slideHeight = pptPres.PageSetup.slideHeight
' 若页面设置获取失败,用母版尺寸兜底
If slideWidth = 0 Or slideHeight = 0 Then
Set pptMaster = pptPres.SlideMaster
slideWidth = pptMaster.Width
slideHeight = pptMaster.Height
End If
On Error GoTo 0
' 11. 循环插入图片到对应幻灯片(1张图片对应1页)
For i = 1 To picFiles.Count
' 11.1 检查幻灯片是否存在,不存在则新建空白幻灯片
If i > pptPres.Slides.Count Then
On Error Resume Next
Set pptSlide = pptPres.Slides.Add(Index:=i, Layout:=ppLayoutBlank)
If Err.Number <> 0 Then
MsgBox "错误:创建第" & i & "张幻灯片失败,将停止插入后续图片!", vbCritical, "幻灯片创建错误"
Exit For
End If
On Error GoTo 0
Else
Set pptSlide = pptPres.Slides(i)
End If
' 11.2 插入图片并设置自适应大小
On Error Resume Next
Set picShape = pptSlide.Shapes.AddPicture(FileName:=picFiles(i), LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0,
Top:=0, Width:=slideWidth, Height:=slideHeight) ' 高度匹配幻灯片
' 11.3 校验图片插入结果
If Err.Number <> 0 Then
MsgBox "警告:第" & i & "张图片插入失败!" & vbCrLf & "图片路径:" & picFiles(i), vbExclamation, "插入错误"
Else
' 可选:将图片置于底层,避免覆盖PPT原有内容
picShape.ZOrder msoSendToBack
End If
On Error GoTo 0
DoEvents ' 释放系统资源,避免循环卡顿
Next i
' 12. 操作完成提示
Dim msg As String
If picFiles.Count > pptPres.Slides.Count Then
msg = "操作完成!" & vbCrLf & _
"成功插入 " & pptPres.Slides.Count & " 张图片(PPT共" & pptPres.Slides.Count & "页)!" & vbCrLf & _
"剩余 " & picFiles.Count - pptPres.Slides.Count & " 张图片因无对应幻灯片未插入。"
Else
msg = "操作完成!" & vbCrLf & "成功插入 " & picFiles.Count & " 张图片到PPT每一页!"
End If
MsgBox msg & vbCrLf & "目标PPT:" & targetPPTPath & vbCrLf & "图片来源:" & picFolder, vbInformation, "插入完成"
' 13. 释放所有对象(避免内存占用)
Set file = Nothing
Set fso = Nothing
Set picFiles = Nothing
Set picShape = Nothing
Set pptSlide = Nothing
Set pptMaster = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
' 辅助函数:按文件名升序排序图片路径集合(确保插入顺序正确)
Private Sub SortPicCollection(col As Collection, fso As Object)
Dim i As Integer, j As Integer
Dim tempPath As String
' 冒泡排序(不区分文件名大小写,避免"A.jpg"排在"a.jpg"之后)
For i = 1 To col.Count - 1
For j = i + 1 To col.Count
If LCase(fso.GetFileName(col(i))) > LCase(fso.GetFileName(col(j))) Then
tempPath = col(i)
col(i) = col(j)
col(j) = tempPath
End If
Next j
Next i
End Sub
|
|