[VBA][办公]以表格形式在word中插入多个图片

当需要将数十张图片批量以表格形式插入word中,并需要在下面显示文件名时,本工具能够发挥这样的功能,按照您的需求自动进行调整,实现自动插入图片于表格中,并按比例调整大小。

当需要将数十张图片批量以表格形式插入word中,并需要在下面显示文件名时,本工具能够发挥这样的功能,按照您的需求自动进行调整,实现自动插入图片于表格中,并按比例调整大小。

Sub 每行插入表格n个图()
     On Error Resume Next
     Application.ScreenUpdating = False
 Dim D As FileDialog, a, P As InlineShape, t As Table
 If Selection.Information(wdWithInTable) = True Then MsgBox "请将光标置于表格之外!": Exit Sub
     With Application.FileDialog(msoFileDialogFilePicker)
         .Title = "请选择..."
         If .Show = -1 Then
         n = InputBox("请输入表格的列数:", "列数", 3)
         M = .SelectedItems.Count
         Debug.Print "共有" & M & "个图片"; M
    h = IIf(M / n = Int(M / n), 2 * M / n, 2 * (Int(M / n) + 1))
    Set t = ActiveDocument.Tables.Add(Selection.Range, h, n)
         t.Borders.Enable = True
 t.Borders.OutsideLineStyle = wdLineStyleDouble
             For Each a In .SelectedItems
                 B = Split(a, "\")(UBound(Split(a, "\")))
                 C = Split(B, ".")(0)
                 Set P = Selection.InlineShapes.AddPicture(FileName:=a, SaveWithDocument:=True)
                 With P
                     w = .Width
                     .Width = Int(410 / n)
                     .Height = .Width * .Height / w
                 End With
                 i = i + 1
                 Selection.MoveLeft wdCharacter, 1
                 Selection.MoveDown wdLine, 1
                 Selection.TypeText C
 Selection.Cells(1).Select
 Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '决定了首行居中
Selection.HomeKey
 Selection.MoveDown wdLine, -1
                 Selection.MoveRight wdCharacter, 2
                 Debug.Print i, n
                 If i = Val(n) Then
                     Selection.MoveRight wdCharacter, 1
                     Selection.Cells(1).Select
                     Selection.EndKey
                     Selection.MoveDown wdLine, 1
                     i = 0
                 End If
             Next
         End If
     End With
     Application.ScreenUpdating = True
 End Sub

[VBA][办公]以表格形式在word中插入多个图片

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索