简体   繁体   English

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

[英]MS Word, VBA, How to select a paragraph within a cell within a table?

I'm new to using VBA to write macros within MS Word.我是使用 VBA 在 MS Word 中编写宏的新手。 I've worked out how to select the cell within the table, but it doesn't appear I can use the paragraph object with it... or, more likely, I'm doing it wrong.我已经研究出如何选择表格中的单元格,但似乎我不能将段落对象与它一起使用……或者,更有可能的是,我做错了。

Essentially, what I'm trying to do, it look for phrase "as follows:" within all the paragraphs of Cell (13,2) of Table(1).本质上,我正在尝试做的是,它会在表 (1) 的单元格 (13,2) 的所有段落中查找短语“如下:”。 If it finds it, I want to see if the next thing that happens after that phrase is a new paragraph with a bullet.如果找到了,我想看看在该短语之后发生的下一件事情是否是带有项目符号的新段落。 If it is, great, nothing more to do.如果是,那太好了,没什么可做的。 If it isn't, then do a new paragraph with a bullet.如果不是,那么用项目符号做一个新的段落。

I'm just not sure how to go about this, particularly determining if there is already a bullet or not.我只是不确定如何解决这个问题,特别是确定是否已经有子弹。

Hoping someone can throw some light on the subject.希望有人可以对这个主题有所了解。 I'll keep plugging away in the meantime.在此期间,我会继续插电。 :) :)

UPDATE: I've gotten this far where it inserts a return and I was hoping would insert a bullet but it is inserting a bullet in numerous spaces in that Cell rather than after the vbCr:更新:我已经走到这一步了,它插入了一个返回,我希望能插入一个项目符号,但它是在该单元格的多个空格中而不是在 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

I've modified your code sample and this works for me.我已经修改了您的代码示例,这对我有用。 Since you already declare and assign a Range to BIOCell you can use that throughout your macro to identify the cell contents.由于您已经声明并为 BIOCell 分配了一个范围,您可以在整个宏中使用它来识别单元格内容。 There's no need to use the "Like" test since Range.Find.Execute returns True if successful, otherwise False.没有必要使用“Like”测试,因为Range.Find.Execute如果成功则返回 True,否则返回 False。 When Find is successful, the Range will change to what has been found (in other words it's no longer the entire cell).当 Find 成功时,范围将更改为已找到的内容(换句话说,它不再是整个单元格)。

Trying to replace with a paragraph mark isn't working as you wish.尝试用段落标记替换不能如您所愿。 Since you need to do something that can't be done with Find/Replace anyway (the bullets) simply add the paragraph mark if Find is successful, put the Range focus at the end of the cell, than apply the Bullets formatting.由于您需要做一些无论如何都无法通过查找/替换(项目符号)完成的事情,如果查找成功,只需添加段落标记,将范围焦点放在单元格的末尾,而不是应用项目符号格式。 (Note that there's no need to use Selection if you have the Range object.) (请注意,如果您有 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

If the table cell already has paragraphs of text and you want everything after the Find term to be bulleted, then the code could look like the example that follows.如果表格单元格已经有文本段落,并且您希望查找术语之后的所有内容都带有项目符号,那么代码可能类似于下面的示例。

In this case, a second Range object is used to perform the Find, while BIOCell remains assigned to the entire cell.在这种情况下,第二个 Range 对象用于执行 Find,而 BIOCell 仍然分配给整个单元格。 (Always use the Duplicate property to make a "copy" of a Range that can be used independently. Range is an anamoly in the Office object models: Range = Range makes both Ranges identical - if you change the position of one, the position of the other changes, as well.) (始终使用Duplicate属性来制作可以独立使用的 Range 的“副本”。Range 在 Office 对象模型中是一个异常: Range = Range使两个 Ranges相同- 如果更改一个的位置,则其他变化也是如此。)

Once Find is successful, the findRange is collapsed to the end of the Find term and moved one paragraph further (to the first paragraph following the found text).一旦 Find 成功,findRange 将折叠到 Find 术语的末尾并进一步移动一个段落(到找到的文本之后的第一段)。 The end of the Range is then extended to the end of the cell (end of BIOCell), then moved back a couple of characters so that it doesn't include the end-of-cell markers.然后将 Range 的末尾扩展到单元格的末尾(BIOCell 的末尾),然后向后移动几个字符,使其不包括单元格结束标记。 (Otherwise the bullets would be applied to the entire cell instead of up through the last paragraph of the cell.) (否则,项目符号将应用于整个单元格,而不是通过单元格的最后一段。)

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

Try:尝试:

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

Unlike Cindy's code, the above will insert a bullet paragraph regardless of whether the 'as follows:' string terminates with a paragraph break (or anything other than a space) when the following paragraph isn't a bulleted one.与 Cindy 的代码不同,上面的代码将插入一个项目符号段落,无论“如下:”字符串是否以段落分隔符(或空格以外的任何其他内容)终止,当以下段落不是项目符号段落时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM