
https://wwgo.lanzouo.com/iHkTE2z6uicj
密码:52pj
ennnnnn,大概就这样吧,下班了!~
[Asm] 纯文本查看 复制代码 Sub ReplaceBackgroundsWithImages()
Dim doc As Document
Dim folderPath As String, imageFolder As String
Dim fileName As String, imageName As String
Dim fileList As Variant, imageList As Variant
Dim i As Integer, pageCount As Integer
Dim section As Section
' 设置文件夹路径(修改为你的路径)
folderPath = "C:\Documents\" ' 文档文件夹
imageFolder = "C:\Backgrounds\" ' 图片文件夹
' 获取文档列表
fileName = Dir(folderPath & "*.docx")
fileList = Array()
Do While fileName <> ""
ReDim Preserve fileList(UBound(fileList) + 1)
fileList(UBound(fileList)) = fileName
fileName = Dir()
Loop
' 获取图片列表(按文件名排序)
imageName = Dir(imageFolder & "*.jpg")
imageList = Array()
Do While imageName <> ""
ReDim Preserve imageList(UBound(imageList) + 1)
imageList(UBound(imageList)) = imageName
imageName = Dir()
Loop
' 检查图片数量是否足够
If UBound(imageList) < 11 Then
MsgBox "图片不足12张,请检查文件夹!"
Exit Sub
End If
' 遍历每个文档
For i = LBound(fileList) To UBound(fileList)
Set doc = Documents.Open(folderPath & fileList(i))
' 遍历文档的每一节(假设每节对应一页)
For Each section In doc.Sections
' 替换页眉/页脚背景(根据实际需求调整)
With section.Headers(wdHeaderFooterPrimary).Range
.Fill.Visible = msoTrue
.Fill.UserPicture (imageFolder & imageList((i - 1) Mod 12 + 1)) ' 循环使用12张图片
End With
Next section
' 保存并关闭文档
doc.Save
doc.Close
Next i
MsgBox "背景图片替换完成!"
End Sub
|