|
Option Explicit
Sub test()
Dim wdApp As Object, strFileName$, strPath$
Dim ar, i&, j&, strSaveName$, strPicName$, dWidth As Double
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "明星档案.docx"
If Dir(strFileName) = "" Then MsgBox "模板文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
On Error Resume Next
ar = [A1].CurrentRegion.Value
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
For i = 2 To UBound(ar)
With wdApp.documents.Open(strFileName)
strSaveName = strPath & ar(i, 1)
For j = 1 To UBound(ar, 2)
With .Content.Find
.ClearFormatting
.Text = "数据" & Format(j, "000")
.Forward = True
.Execute
If .Found = True Then .Parent.Text = ar(i, j)
End With
Next j
strPicName = strPath & "照片\" & ar(i, 1) & ".jpg"
If Dir(strPicName) <> "" Then
With .tables(1).Range.Cells(7)
dWidth = .Width
With .Range.InlineShapes.AddPicture(strPicName, , True)
.LockAspectRatio = True
.Width = dWidth
End With
End With
End If
.SaveAs strSaveName: .Close
End With
Next i
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|