|
做工程报告用,一般会处理出来很多照片,一张一张往word里粘贴很不方便。希望大神能帮忙写个命令宏,我只需要在word里指定位置写上我需要的照片名称,然后通过宏,将某个文件夹内的相同名称的照片替换掉word里的文字,并且该图片被设置为指定大小、居中等格式,最好照片底下还能再次显示照片名称,作为图片说明。
从文件夹导入图片,并在图片底下显示照片名称的宏我已经有,提供在下面,望大神帮帮忙
Sub 插入图片()
Dim myfile As FileDialog
Set myfile = Application.FileDialog(msoFileDialogFilePicker)
With myfile
.InitialFileName = "C:\"
If .Show = -1 Then
For Each fn In .SelectedItems
Set mypic = Selection.InlineShapes.AddPicture(FileName:=fn, SaveWithDocument:=True)
mypic.Width = 400 '根据需要设置
mypic.Height = 300
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Selection.Text = Basename(fn) '函数取得文件名
Selection.EndKey
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Next fn
Else
End If
End With
Set myfile = Nothing
End Sub
Function Basename(FullPath) '取得文件名
Dim x, y
Dim tmpstring
tmpstring = FullPath
x = Len(FullPath)
For y = x To 1 Step -1
If Mid(FullPath, y, 1) = "\" Or _
Mid(FullPath, y, 1) = ":" Or _
Mid(FullPath, y, 1) = "/" Then
tmpstring = Mid(FullPath, y + 1)
Exit For
End If
Next
Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function
|
|