Word VBA 2010 - 格式化表格单元格中的最后一段落

4
我有一个项目,我将其从MadCap Flare导出到Word 2010,并使用VBA脚本更新文档的格式。我试图检查文档中每个段落的样式,如果它与特定样式匹配,则应用多级列表格式。
它几乎可以无问题地工作。当段落作为表格单元格中的最后一个段落时,问题就会出现。在这种情况下,范围包括单元格结束标记(因此范围包括单元格中的每个段落),因此更改会应用于单元格中的每个段落,而不仅仅是最后一个段落。
我使用的代码如下:
For Each iPara In ActiveDocument.Paragraphs
    With iPara.Range
        If iPara.Style.NameLocal = "div_NoteText" Then
            .ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
            ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
            ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
            DefaultListBehavior:=wdWord10ListBehavior, ApplyLevel:=1
        End If
    End With
Next

我需要做哪些改动才能让这个在表格单元格中的最后一段起作用?

感谢您的欢迎和反馈。也许一些图片可以更好地描述问题:http://imgur.com/a/TVSBX 1. 在任何宏运行之前。2. 宏已正确修改了表格单元格中的第二段落。3. 宏正在选择表格单元格中的第三段落(因为它包括单元格结束标记,然后突出显示所有段落)。4. 格式应用于整个单元格,而不是期望的效果。 - jarch3r
由于网络限制,我无法查看链接,但我创建了一个包含2列3行的表格的Word文档,并将两个段落放在单元格(2,2)中,您的代码将这两个段落分别处理。如果您打开符号 ctrl + shift + *,您会在单元格中看到多个段落符号还是只有一个? - jmstoker
这是我所期望的。我在for循环中添加了MsgBox iPara.Range,它单独列出每个段落,这让我想到可能与你的div_NoteText条件有关。那是内置的NameLocal还是别名? - jmstoker
当我在循环中添加了 MsgBox iPara.Range 后,对于单元格中的最后一段落,它只显示了该段落,但是在消息框的下一行还显示了一个框形图标。我认为这代表了单元格结束字符,当选择它时,整个单元格实际上包含在范围内。 - jarch3r
也许当你在循环中尝试使用iPara.Range.Select时,你会看到整个单元格是如何被选中的。 - jarch3r
显示剩余3条评论
3个回答

4

“单元格末尾”标记为Chr(13) + Chr(7),因此您可以使用类似下面的代码检测位于单元格末尾的段落:

Sub Tester()
Dim EOC As String
Dim p As Paragraph
Dim rng As Range

    EOC = Chr(13) & Chr(7)

    For Each p In ActiveDocument.Paragraphs

        If Len(p.Range.Text) > Len(EOC) And p.Range.Text Like "*" & EOC Then
               Set rng = p.Range

               'commenting out next line will select the whole cell
               rng.MoveEnd wdCharacter, -1  

               rng.Select
               MsgBox "Found paragraph at end of cell..."
        End If

    Next p

End Sub

感谢提供有关EOC的信息。这肯定会帮助我找到解决方案,明天我将有更多时间进行测试。不幸的是,我在使用rng.MoveEnd wdCharacter, -1时没有成功,因为它似乎无法取消选择Word中的EOC字符(甚至无法修改范围),导致整个单元格被选中,而我只想要最后一段落。类似的EOC材料:http://www.vbaexpress.com/kb/getarticle.php?kb_id=631 - jarch3r
在我的测试中(使用我发布的代码),它正常工作:当选择表格单元格末尾的段落时,只选择了该段落 - 而没有选择单元格中的其他内容。 - Tim Williams
按照广告所述正常工作。感谢您的回答和高效表达。在测试MoveEnd方法时,我一定犯了一些简单的编码错误。 - jarch3r
2
这太棒了,令人惊叹,解决了我的问题!请注意,chr(7)不会出现在Range.Characters数组中,它只会出现在Range.Text字符串中。此外,Range.End-Range.Start不反映这个额外的chr(7)。 - dale

1
该程序将通过首先扫描所有不在表格中的段落,然后检查所有表格并仅对每个单元格中的最后一个段落应用更改来检查所有段落。
检查段落。
Sub CheckParagraphs()

  For Each iPara In ActiveDocument.Paragraphs
      With iPara.Range
          If Selection.Information(wdWithInTable) = False Then
              If iPara.Style.NameLocal = "div_NoteText" Then
                .ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
                ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
                ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
                DefaultListBehavior:=wdWord10ListBehavior, ApplyLevel:=1
              End If
          End If
      End With
  Next

  CheckTables
End Sub

检查表格
Sub CheckTables()

  Dim oPara As Range
  Dim count As Integer
  For Each t In ActiveDocument.Tables
    For Each r In t.Rows
        For Each c In r.Cells
            With c.Range
                'Only act on the last paragraph
                With .Paragraphs(.Paragraphs.count).Range
                    If .Style.NameLocal = "div_NoteText" Then
                        .ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
                        ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
                        ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
                        DefaultListBehavior:=wdWord10ListBehavior
                        .SetListLevel Level:=1
                    End If
                End With
            End With
        Next
    Next
  Next
End Sub

我之前也考虑过按照你在这里所做的方式分离案例,但我仍然不知道如何处理表格案例。谢谢你提供了这个很棒的例子,在明天我会去测试并练习。我的唯一担心是.Paragraphs(.Paragraphs.count).Range中仍包含EOC字符,我还没有想出如何从范围中排除它。我尝试了rng.MoveEnd wdCharacter, -1myRange.Collapse Direction:=wdCollapseEnd,但似乎都未能修改范围(我不确定原因)。待明天更多测试后我会更新。 - jarch3r
再次感谢您与我一起解决问题。正如您所知,另一个答案完美地解决了问题。 - jarch3r

-1
这是我在MS Word中修剪表格范围的方法
Sub remove()
    Set aa = Selection.Tables(1)
    dim bb, cc as integer
    bb = Selection.Tables(1).Rows.Count
    cc = Selection.Tables(1).Columns.Count
    
    For i = 1 To bb
    For j = 1 To cc
        '1st
        Set dd = aa.Cell(i, j).Range.Paragraphs(1)
        If Len(dd) < 2 Then
        dd.Range.Delete
        End If
        
        'last
        ee = aa.Cell(i, j).Range.Paragraphs.Count
        ff = Right(aa.Cell(i, j).Range, 3)
        If Asc(ff) = 13 Or Asc(ff) = 32 Or Asc(ff) = 46 Then
        aa.Cell(i, j).Select
        Selection.Paragraphs(ee).Range.Characters.Last.Previous.Delete
        End If
    Next
    Next
End Sub

我不清楚这个回答如何解决问题,问题是关于如何在表格单元格中格式化最后一个段落。你的代码似乎是在删除段落/字符,而不是应用格式化? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接