|
*** 楼主,上楼代码有问题!请用本楼代码试试:
- Sub Word表格批量插入图片_top()
- Dim D As FileDialog, a, P As InlineShape, t As Table, n$, mc$, m&, h$, w!, i&, b$, c$, j&
- Documents.Add
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "请选择包含图片的文件夹,打开后选择所有图片!"
- If .Show = -1 Then
- ' n = InputBox("请输入表格的列数:", "列数", 1)
- ' mc = InputBox("是否同时插入名称?", "名称", 1)
- n = 1: mc = 1
-
- m = .SelectedItems.Count
- If mc = 1 Then
- h = IIf(m / n = Int(m / n), 2 * m / n, 2 * (Int(m / n) + 1))
- Else
- h = IIf(m / n = Int(m / n), m / n, (Int(m / n) + 1))
- End If
- Set t = ActiveDocument.Tables.Add(Selection.Range, h, n)
- With t
- .Borders.Enable = True
- .Borders.InsideColor = wdColorLightBlue '内部线条显色'
- .Borders.OutsideColor = wdColorLightBlue '外部线条显色
- ' .Borders.OutsideLineStyle = wdLineStyleDouble '外部线条样式
- .Borders.OutsideLineWidth = wdLineWidth075pt '线条宽度
- End With
-
- For Each a In .SelectedItems
- If mc = 1 Then
- b = Split(a, "")(UBound(Split(a, ""))) '或修改成b.name
- c = Split(b, ".")(0)
- With Selection
- .TypeText c '键入文件名
- .Cells(1).Select
- .Font.Bold = True
- .Font.Size = 14
- .ParagraphFormat.Alignment = wdAlignParagraphCenter '决定了首行居中
- .HomeKey
- .MoveDown wdLine, 1 '光标下移
- End With
- Else
- Selection.MoveRight wdCharacter, 1 '光标右移两个单元,到右边单元格
- End If
- 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
-
- If i = Val(n) Then
- If mc = 1 Then
- With Selection
- .MoveLeft wdCharacter, 1
- .Cells(1).Select
- .Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
- .EndKey
- .MoveDown wdLine, 1
- End With
- i = 0
- Else
- Selection.MoveRight wdCharacter, 1
- i = 0
- End If
- End If
- Next
- End If
- End With
-
- '''
- With ActiveDocument
- With .PageSetup
- .TopMargin = CentimetersToPoints(4.5) '上边距可修改
- .BottomMargin = CentimetersToPoints(4.5) '下边距可修改
- End With
- Do
- j = j + 2
- .Tables(1).Rows(j).HeightRule = wdRowHeightExactly
- .Tables(1).Rows(j).Height = CentimetersToPoints(9) '图片所在行高可修改
- Loop Until j = 2 * m
- End With
-
- '''最后一磅
- With ActiveDocument.Paragraphs
- With .Last.Range
- If .Text = vbCr Then .Delete
- End With
- With .Last.Range
- If .Text = vbCr Then
- With .Font
- .Size = 1
- .Kerning = 0
- .DisableCharacterSpaceGrid = True
- End With
- With .ParagraphFormat
- .LineSpacing = LinesToPoints(0.06)
- .AutoAdjustRightIndent = False
- .DisableLineHeightGrid = True
- End With
- End If
- End With
- End With
- Selection.HomeKey 6
- End Sub
复制代码 |
|