如何在每个图像后提示第一段

时间:2019-02-13 01:28:16

标签: vba ms-word

我试图在活动文档中的每个图像之后提示每个段落。由于某种原因,提示会弹出空白。

Sub Example1()
Dim intCount As Integer
Dim i As Integer
Dim strCaption As String

'loop through inline shapes
For i = 1 To ActiveDocument.InlineShapes.Count
'check if the current shape is an picture
    If ActiveDocument.InlineShapes.Item(i).Type = wdInlineShapePicture Then
       strCaption = Selection.Paragraphs(1).Range
       MsgBox strCaption
    End If
Next i
End Sub

2 个答案:

答案 0 :(得分:0)

此代码可能会起作用,具体取决于InlineShapes的放置方式。该代码假定每张图片都在其自己的段落中,然后挑选出下一张。

Sub Example1()

    Dim i As Integer
    Dim strCaption As String
    Dim Rng As Range

    With ActiveDocument.InlineShapes
        'loop through inline shapes
        For i = 1 To .Count
            With .Item(i)
                'check if the current shape is a picture
                If .Type = wdInlineShapePicture Then
                    Set Rng = .Range.Paragraphs(1).Range
                    With Rng
                        Do
                            .Collapse wdCollapseEnd
                            .MoveEnd wdParagraph
                        Loop While Len(Trim(.Text)) = 1 And _
                                   .End < .Document.Content.End
                        strCaption = Rng.Text
                   End With
                   MsgBox strCaption
                End If
            End With
        Next i
    End With
End Sub

答案 1 :(得分:0)

如果您要查找的文本与inlineshape在同一段落中,则可以使用以下代码:

Sub Demo()
Dim iSHp As InlineShape, Rng As Range
For Each iSHp In ActiveDocument.InlineShapes
  Set Rng = iSHp.Range.Paragraphs(1).Range
  With Rng
    .Start = iSHp.Range.End
    MsgBox .Text
  End With
Next
End Sub

如果您要查找的文本位于inlineshape之后的下一段中,则可以使用如下代码:

Sub Demo()
Dim iSHp As InlineShape, Rng As Range
For Each iSHp In ActiveDocument.InlineShapes
  Set Rng = iSHp.Range.Paragraphs(1).Range
  With Rng
    .Collapse wdCollapseEnd
    .MoveEnd wdParagraph, 1
    MsgBox .Text
  End With
Next
End Sub