MS Word,VBA,如何在表中的单元格中选择一个段落



我是在MS Word中使用VBA编写宏的新手。我已经弄清楚了如何在表中选择单元格,但是看来我可以将段落对象与它一起使用……或者更有可能,我做错了。

本质上,我要做的是在表(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

我已经修改了您的代码样本,这对我有用。由于您已经声明并为BioCell分配了一个范围,因此可以在整个宏中使用该范围来识别单元格内容。无需使用"喜欢"测试,因为Range.Find.Execute如果成功,则返回true,否则为false。当发现成功时,范围将变为已找到的东西(换句话说,它不再是整个单元格(。

试图用段落标记替换不按照您的意愿进行。由于您需要做任何无法通过查找/替换来完成的事情(子弹(只需添加段落标记,如果找到成功,请将范围焦点放在单元的末端,而不是应用子弹格式化。(请注意,如果您具有范围对象,则无需使用选择。(

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

如果表单元格已经有文本段落,并且您希望找到术语后的所有内容,则代码看起来像以下示例。

在这种情况下,第二个范围对象用于执行发现,而Biocell仍将分配给整个单元格。(始终使用Duplicate属性来制作可以独立使用的范围的"副本"。范围是Office对象模型中的一个血管:Range = Range使两个范围相同 - 如果您更改了一个位置,另一个位置也会变化。(

一旦找到成功,Findrange就会折叠到查找术语的末尾,然后进一步移动一个段落(遵循发现文本的第一段(。然后将范围的末端扩展到单元格的末端(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

尝试:

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

与辛迪的代码不同,上面的段落将插入子弹段,无论"如下:'字符串在以下段落不是段落不是子弹时都以段落断裂(或其他空间(终止。

最新更新