将边框应用于 InlineShape 适用于 table 它所在的而不是图像

Applying borders to InlineShape applies to table it resides in rather than image

所以我有一些 InlineShapes 驻留在 WORD 中的 table 中。我正在尝试获取 table 第三列中的所有 InlineShapes 并在它们周围应用边框。不幸的是,对于我的生活,我无法弄清楚如何将它应用于图片而不是 table 的单元格。

这是我的代码:

ActiveDocument.Tables(1).Columns(3).Select

For Each iPicture In Selection.InlineShapes
    With iPicture
        .Borders.Enable = True
        .Borders.OutsideColor = wdColorRed
        .Borders.OutsideLineWidth = wdLineWidth150pt
        .Borders.OutsideLineStyle = wdLineStyleSingle
    End With
Next

我也尝试将以下代码放入 With iPicture 但似乎没有什么不同,仍然在单元格而不是图片周围绘制边框。

With .Borders(wdBorderLeft)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = wdColorRed
End With

With .Borders(wdBorderRight)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = wdColorRed
End With

With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = wdColorRed
End With

With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = wdColorRed
End With

我的测试没有定论。有些对象有边框;与其他人一起,边框应用于单元格。

然而,在所有后一种情况下,如果我在对象之后(在单元格的末尾)放置一个 space,则可以正确应用边框。

因此,我怀疑它与单元格末尾的隐藏段落标记有关,该标记有时会以某种方式被 InlineShape 对象拾取。

如果单元格中的字符数小于或等于两个(仅包含 InlineShape 和结束-细胞标记)。

插入 space 折叠选择。因此,table 和选定的列会在开头注明,并在该操作后重新选择该列。如果 space 对您来说是个问题,添加代码,在创建边框后删除它。

Sub BorderInlineShapes()
    Dim sel As Word.Selection
    Dim ils As Word.InlineShape
    Dim rng As Word.Range
    Dim colNr As Long
    Dim tbl As Word.Table

    Set sel = Selection
    If sel.Tables.Count = 1 Then
        Set tbl = sel.Tables(1)
        colNr = sel.Information(wdEndOfRangeColumnNumber)
        For Each ils In sel.InlineShapes
            If Not ils.Borders.Enable Then ils.Borders.Enable = True
            If ils.Range.Cells.Count > 0 Then
                Set rng = ils.Range
                If rng.Cells(1).Range.Characters.Count <= 2 Then
                    rng.InsertAfter " "
                    tbl.Columns(colNr).Select
                End If
            End If
            ils.Borders.OutsideColor = wdColorRed
            ils.Borders.OutsideLineWidth = wdLineWidth075pt
        Next
    End If
End Sub