VBA向表格中插入图片并设置其环绕格式为环绕型

Sub 宏1()
'
' 宏1 宏
'
'
    Application.ScreenUpdating = False

    Dim oTbl As Table, i As Long, j As Long, k As Long

    'Select and insert the Pics
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "请选择 3 张或 4 张图片以插入"
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
        .FilterIndex = 2
        .InitialFileName = ActiveDocument.Path
        
        If .Show = -1 Then
            If .SelectedItems.Count < 3 Or .SelectedItems.Count > 4 Then
                MsgBox ("图片数量错误,只能选择 3 张或 4 张图片!")
            Else
                '在主表格第4行,第2列中插入了一个 2*2 的子表格并设置其表格线隐藏
                Set oTbl = ActiveDocument.Tables(1).Rows(4).Cells(2).Tables(1)
                For i = 1 To .SelectedItems.Count
                    j = Int((i + 1) / 2)
                    k = (i - 1) Mod 2 + 1

                    ActiveDocument.InlineShapes.AddPicture _
                    FileName:=.SelectedItems(i), LinkToFile:=False, _
                    SaveWithDocument:=True, Range:=oTbl.Rows(j).Cells(k).Range
                Next
                ' 插入的图片默认状态为嵌入型,只显示一小条,设置其环绕格式为:四周型              
                Dim objInSp As InlineShape
                Dim objSp As Shape
                For Each objInSp In ActiveDocument.InlineShapes
                    Set objSp = objInSp.ConvertToShape
                    objSp.WrapFormat.Type = wdWrapSquare
                Next
                Set objInSp = Nothing
                Set objSp = Nothing
            End If
        End If
    End With
    Application.ScreenUpdating = True
End Sub
posted @ 2024-04-15 08:29  汉学  阅读(46)  评论(0)    收藏  举报