|
本帖最后由 humanmagic 于 2019-12-24 21:28 编辑
代码放Excel里运行,Excel和Word文件在同一目录,代码运行期间Word不能打开。
漏洞一:Excel文字太长到Word中会显示不全漏洞二:Excel形状不会复制去Word
其它待补充
- Sub demo()
- Application.ScreenUpdating = False
- Dim wordapp As Object, arr, i As Long, j As Long
- 'Word图片位置和宽高,四组
- Dim t1 As Double, t2 As Double, l1 As Double, l2 As Double
- Dim w1 As Double, w2 As Double, h1 As Double, h2 As Double
- Dim t3 As Double, t4 As Double, l3 As Double, l4 As Double
- Dim w3 As Double, w4 As Double, h3 As Double, h4 As Double
- l1 = -4.9: t1 = 0.6: h1 = 126.75: w1 = 217.5
- l2 = -3.9: t2 = 0.6: h2 = 126.75: w2 = 222
- l3 = -4.9: t3 = 0.75: h3 = 126.75: w3 = 217.5
- l4 = -3.9: t4 = 0.75: h4 = 126.75: w4 = 222
- 'Excel图片所在单元格判断
- Dim le1 As Double, le2 As Double, le3 As Double, leMid As Double
- Dim to1 As Double, to2 As Double, to3 As Double, to4 As Double, toMid As Double
- leMid = (Range("H7").Left - Range("G7").Left) / 2
- le1 = Range("G7").Left + leMid: le2 = Range("H7").Left + leMid: le3 = Range("I7").Left + leMid
- toMid = (Range("G5").Top - Range("G4").Top) / 2
- to1 = Range("G3").Top + toMid: to2 = Range("G4").Top + toMid: to3 = Range("G5").Top + toMid: to4 = Range("G6").Top + toMid
- '图片复制到Word的位置
- Dim tableIndex As Long, cellIndex As Long
- arr = Range("D3:F7").Value '工作表数据
- On Error Resume Next
- Set wordapp = CreateObject("Word.Application")
- wordapp.Visible = True
- With wordapp.documents.Open(ActiveWorkbook.Path & "\城市发展组月检查照片报告.docx")
- j = .Shapes.Count '删除Word所有Shapes
- For i = j To 1 Step -1
- .Shapes(i).Delete
- Next i
- For i = 1 To UBound(arr)
- With .tables(i)
- .Cell(2, 2).Range.Text = arr(i, 1)
- .Cell(3, 2).Range.Text = arr(i, 2)
- .Cell(1, 3).Range.Text = arr(i, 3)
- End With
- Next i
- '图片操作
- For i = 1 To ActiveSheet.Shapes.Count
- If ActiveSheet.Shapes(i).Type <> 13 Then GoTo continnue
- ActiveSheet.Shapes(i).Select
- Selection.Copy
- Select Case ActiveSheet.Shapes(i).Top
- Case Is < to1
- tableIndex = 1
- Case Is < to2
- tableIndex = 2
- Case Is < to3
- tableIndex = 3
- Case Is < to4
- tableIndex = 4
- Case Is > to4
- tableIndex = 5
- End Select
- Select Case ActiveSheet.Shapes(i).Left
- Case Is < le1
- cellIndex = 1
- Case Is < le2
- cellIndex = 2
- Case Is < le3
- cellIndex = 3
- Case Is > le3
- cellIndex = 4
- End Select
- Select Case cellIndex
- Case 1
- .tables(tableIndex).Cell(4, 1).Range.Paste
- .InlineShapes(.InlineShapes.Count).ConvertToShape.WrapFormat.Type = 6
- With .Shapes(.Shapes.Count)
- .LockAspectRatio = False
- .Top = t1: .Left = l1: .Height = h1: .Width = w1
- End With
- Case 2
- .tables(tableIndex).Cell(4, 2).Range.Paste
- .InlineShapes(.InlineShapes.Count).ConvertToShape.WrapFormat.Type = 6
- With .Shapes(.Shapes.Count)
- .LockAspectRatio = False
- .Top = t2: .Left = l2: .Height = h2: .Width = w2
- End With
- Case 3
- .tables(tableIndex).Cell(5, 1).Range.Paste
- .InlineShapes(.InlineShapes.Count).ConvertToShape.WrapFormat.Type = 6
- With .Shapes(.Shapes.Count)
- .LockAspectRatio = False
- .Top = t3: .Left = l3: .Height = h3: .Width = w3
- End With
- Case 4
- .tables(tableIndex).Cell(5, 2).Range.Paste
- .InlineShapes(.InlineShapes.Count).ConvertToShape.WrapFormat.Type = 6
- With .Shapes(.Shapes.Count)
- .LockAspectRatio = False
- .Top = t4: .Left = l4: .Height = h4: .Width = w4
- End With
- End Select
- continnue:
- Next i
- .Close -1
- End With
- wordapp.Quit
- If Err.Number <> 0 Then Err.Clear
- On Error GoTo 0
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|