word vba 插入图片

word vba 插入图片,第1张

Sub 批量插入图片()

Dim myfile As FileDialog

Set myfile = Application.FileDialog(msoFileDialogFilePicker)

With myfile

.InitialFileName = "E:\工作文件" ‘这里输入你要插入图片的目标文件夹

If .Show = -1 Then

For Each Fn In .SelectedItems

Selection.Text = Basename(Fn) '这两句移到这里

Selection.EndKey

If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末

Selection.TypeParagraph '在文末添加一空段

Else

Selection.MoveDown

End If

Set MyPic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True) '按比例调整相片尺寸

WidthNum = MyPic.Width

c = 6 '在此处修改相片宽,单位厘米

MyPic.Width = c * 28.35

MyPic.Height = (c * 28.35 / WidthNum) * MyPic.Height

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

执行此代码后,d出的选择对话框, 全选目标文件夹下的所有图片文件之后,点击确定。然后静静的等待电脑完成处理工作,次数word会进入无响应状态。图片越多,无响应的时间越长。

给你一条思路吧。

第一,打开word文件,用录制宏录一段插入图片的代码(这里注意代码的路径和图片名);再录一段保存文件到A目录的代码。备用;

第二,新建一个excel文件,将你A1到A3文件夹里需要建文件的人名全部列到excel的某一列中;

第三,在excel里写vba代码,循环执行这些人名,每个人名都有新建、插入图片、保存这个过程(这里会用到第一步录到的代码)。

请自己尝试学习并应用!

宏代码如下:

Sub Macro1()

ActiveDocument.Tables(1).Cell(3, 1).Select

Selection.InlineShapes.AddPicture FileName:="C:\a.jpg", LinkToFile:=True, SaveWithDocument:=True

End Sub

HF


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11537775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存