MS Word,VBA,如何选择表格内单元格中的段落?

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

我是新手使用VBA在MS Word中编写宏。我已经研究了如何选择表格中的单元格,但似乎没有我可以使用段落对象...或者,更可能的是,我做错了。

基本上,我正在尝试做的,它在表(1)的单元格(13,2)的所有段落中寻找短语“如下:”。如果它找到了,我想看看在该短语之后发生的下一件事是否是带有子弹的新段落。如果是,那很好,没什么可做的。如果不是,那么用子弹做一个新的段落。

我只是不确定如何解决这个问题,特别是确定是否已有子弹。

希望有人可以对这个问题有所了解。在此期间我会继续插手。 :)

更新:我已经到了这么远,它插入一个返回,我希望插入一个子弹但它插入一个子弹在该单元格中的许多空格而不是在vbCr之后:

Dim BIOCell As range
With ActiveDocument
    Set BIOCell = .range(Start:=.Tables(1).Cell(13, 2).range.Start, _
        End:=.Tables(1).Cell(13, 2).range.End)
    BIOCell.Select
End With

With ActiveDocument.Tables(1)
    If .Cell(13, 2).range.Text Like "*as follows:*" Then
        With Selection.Find
            .Text = "as follows: "
            .Replacement.Text = "as follows:" & vbCr
                Selection.range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
                    ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
                    False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
                    wdWord10ListBehavior
            .Execute Replace:=wdReplaceAll
            End With
        Else
            MsgBox "couldn't find it"
    End If
End With
vba ms-word
2个回答
0
投票

尝试:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
With ActiveDocument.Tables(1).Cell(13, 2)
  Set Rng = .Range
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "as follows:"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    If .Find.Found = False Then
      MsgBox "couldn't find it"
      Exit Sub
    End If
    Do While .Find.Found
      If .InRange(Rng) Then
        If .Characters.Last.Next <> vbCr Then .InsertAfter vbCr & vbCr
        If .Paragraphs.Last.Next.Range.ListFormat.ListType <> wdListBullet Then
          If Len(.Paragraphs.Last.Next.Range.Text) > 1 Then .InsertAfter vbCr
          .Paragraphs.Last.Next.Range.ListFormat.ApplyListTemplateWithLevel _
            ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), _
            ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
            DefaultListBehavior:=wdWord10ListBehavior
        End If
      Else
        Exit Do
      End If
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub

与Cindy的代码不同,上面将插入一个子弹段落,无论“如下:”字符串是否以段落(或空格以外的任何内容)终止,而下一段不是项目符号。


0
投票

我修改了你的代码示例,这对我有用。由于您已经为BIOCell声明并分配了一个范围,因此可以在整个宏中使用它来识别单元格内容。没有必要使用“赞”测试,因为如果成功则Range.Find.Execute返回True,否则返回False。当查找成功时,范围将更改为已找到的内容(换句话说,它不再是整个单元格)。

试图用段落标记替换不能按您的意愿工作。由于你需要做一些无法通过查找/替换完成的事情(子弹),只需添加段落标记,如果查找成功,将范围焦点放在单元格的末尾,而不是应用项目符号格式。 (请注意,如果您有Range对象,则无需使用Selection。)

Sub FindInCellAppendBullets()
    Dim BIOCell As Range
    Dim found As Boolean

    With ActiveDocument
        Set BIOCell = .Range(Start:=.Tables(1).Cell(13, 2).Range.Start, _
            End:=.Tables(1).Cell(13, 2).Range.End)
        BIOCell.Select
    End With

     With BIOCell.Find
        .Text = "as follows: "
        found = .Execute
        If found Then
            BIOCell.InsertParagraphAfter
            BIOCell.Collapse wdCollapseEnd
            BIOCell.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
               ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
               False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
               wdWord10ListBehavior
        Else
            MsgBox "couldn't find it"
        End If
    End With    
End Sub

如果表格单元格已经包含文本段落,并且您希望查找“查找”字词后的所有内容都是项目符号,则代码可能类似于下面的示例。

在这种情况下,第二个Range对象用于执行查找,而BIOCell仍然分配给整个单元格。 (总是使用Duplicate属性来制作可以独立使用的Range的“副本”。范围是Office对象模型中的anamoly:Range = Range使两个范围相同 - 如果你改变一个的位置,其他的变化也是如此。)

一旦查找成功,findRange将折叠到Find项的末尾,并进一步移动一个段落(到找到的文本后面的第一个段落)。然后将Range的末尾扩展到单元格的末尾(BIOCell的结尾),然后移回几个字符,使其不包含单元格结束标记。 (否则子弹将应用于整个单元格,而不是通过单元格的最后一段。)

Sub FindInCellFormatWithBullets()
    Dim BIOCell As Range
    Dim findRange As Range
    Dim found As Boolean

    With ActiveDocument
        Set BIOCell = .Range(Start:=.Tables(1).Cell(13, 2).Range.Start, _
            End:=.Tables(1).Cell(13, 2).Range.End)
        Set findRange = BIOCell.Duplicate
        BIOCell.Select
    End With

     With findRange.Find
        .Text = "as follows: "
        found = .Execute
        If found Then
            findRange.MoveStart wdParagraph, 1
            findRange.End = BIOCell.End - 2
            findRange.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
               ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
               False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
               wdWord10ListBehavior
        Else
            MsgBox "couldn't find it"
        End If
    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.