加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在Word文档中插入图片和图片名称

(2020-07-22 19:00:20)
标签:

word文档插入图片名称

分类: 资料
软件测试过程中,需要将批量生成的DM码及编码,整理到文档中。因此有了这样的需求。
本文的思路是:先用Excel生成编码-DM码清单,再将表格粘到Word中(如果数据量大,会很慢,可以分多次粘贴)。

Excel生成编码-DM码清单
参考文档1中的代码可以直接拿来用,很方便。下面是使用过程:
在Word文档中插入图片和图片名称

在Word文档中插入图片和图片名称

在Word文档中插入图片和图片名称

在Word文档中插入图片和图片名称

在Word文档中插入图片和图片名称

收藏代码:
Public Sub Q()
'开始插入图片
    Application.ScreenUpdating = False
Dim PicName$, pand&, k&, PicPath, i, p, n, PicArr, TitleRow
Dim PicNameCol, PicPath2, PicPath3, TPnameCol, TPCol
    Set PicNameCol = Application.InputBox("请选择图片名称所在列,只能选择单列单元格!", Title:="图片名称所在列", Type:=8)
        '选择的图片名称所在列
    PicCol = PicNameCol.Column '取图片名称所在列列列标
   
    Set TPnameCol = Application.InputBox("请选择图片需要放置的列,只能选择单列单元格!", Title:="图片所在列", Type:=8)
        '选择的图片所在列
    TPCol = TPnameCol.Column '取图片所在列列列标
   
    TitleRow = Val(Application.InputBox("请输入标题行的行数。")) '用户设置总表的标题行数
    If TitleRow < 0 Then MsgBox "标题行必须大于等于零,请重新确认? ": Exit Sub
   
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False '禁止多选文件夹
       If .Show Then PicPath = .SelectedItems(1) Else: Exit Sub
    End With
    If Right(PicPath, 1) <> "" Then PicPath = PicPath & ""
    PicArr = Array(".jpg", ".jpeg", ".bmp", ".png", ".gif") '假定图片格式有5种
    For i = TitleRow 1 To Cells(Rows.Count, PicCol).End(3).Row
        PicPath2 = PicPath
        PicName = Cells(i, PicCol).Value
        If Len(PicName) <> 0 Then '如果PicName不为空
            PicPath3 = PicPath2 & PicName
            pand = 0
            For p = 0 To UBound(PicArr)
                If Len(Dir(PicPath3 & PicArr(p))) Then '如果picpath路径下存在PicName图片
                    ActiveSheet.Shapes.AddPicture PicPath3 & PicArr(p), True, True, _
                    Cells(i, TPCol).Left, Cells(i, TPCol).Top, _
                    Cells(i, TPCol).Width, Cells(i, TPCol).Height
                    pand = 1
                    n = n 1
                End If
            Next
            If pand = 0 Then k = k 1
            End If
    Next
    Application.ScreenUpdating = True
    If k <> 0 Then
        MsgBox "图片插入完成!共有" & k & "张图片未找到,请重新确认源文件! "
    Else
        MsgBox "所有图片插入完成!"
    End If
End Sub

导入图片完成。
参考文档2
调整格式:调整行高;按住Alt拖动图片,调整首个、最后一个图片的位置;“定位条件->对象”选择所有图片,“格式->对齐->纵向分布”调整所有图片位置。
在Word文档中插入图片和图片名称

Word处理
Excel粘贴到Word中效果
在Word文档中插入图片和图片名称
使用分栏,可以让布局更紧凑
在Word文档中插入图片和图片名称


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有