使用 MS Word VBA 获取目标图像标题

问题描述 投票:0回答:1

我想扫描一份报告并突出显示交叉引用。突出显示作为引用的 TypeFields 非常容易。

如何定位引用图像的标题?我不希望它们相互连接(还没有)。

我尝试了

InlineShapes.Caption
,但没有任何结果(
InlineShapes
有效,我可以调整大小和删除图像)。
我还尝试选择字幕,但通过选择,我最终再次得到图像。
我也尝试通过
InlineShape.Text
提取标题,但没有结果。还有
InlineShape.Caption.Text
InlineShape.CaptionLabel.Text
,它们都是无法识别的物体(如果我没记错的话)。

这是我想出的代码,它查找引用,突出显示它们,以及删除图像。

Sub ReferenceHighlight()
    'Define the range as the whole document
    Dim docRange As Range
    Set docRange = ActiveDocument.Range
    'Define all Fields in the Document
    Dim fld As Word.Field
    'Define an incremental integer
    Dim i As Integer
    i = 1
    'Define all Pictures in the Document
    Dim image As InlineShape
    For Each image In docRange.InlineShapes
        image.Delete
    Next image
    For Each fld In docRange.Fields
        If fld.Type = wdFieldRef Then
             fld.Result.HighlightColorIndex = wdYellow
        End If
    Next fld
End Sub
vba ms-word caption
1个回答
0
投票

请尝试一下。

    For Each fld In docRange.Fields
        If fld.Type = wdFieldRef Then
            fld.Select
            Selection.Expand xlLine
            Selection.Range.HighlightColorIndex = wdYellow
        End If
    Next fld

  • 突出显示字幕的更有效方法。 (@Timothy Rylatt在我回答之前发表评论)
Sub HightLightCaption()
    Options.DefaultHighlightColorIndex = wdYellow
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Style = ActiveDocument.Styles("Caption")
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Replacement.Highlight = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.