2014-03-27 63 views
2

我有一个项目,可以从MadCap Flare导出到Word 2010中,并使用VBA脚本更新文档的格式。我试图检查文档中每个段落的样式,然后如果它匹配特定样式,则应用多列表级别格式。Word VBA 2010 - 格式化表格单元格中的最后一段

它几乎没有问题。当段落作为表格单元格的最后一段时,问题就出现了。在这种情况下,范围包括单元格标记的结尾(因此范围包括单元格的每个段落),因此该更改将应用​​于表格单元格中的每个段落,而不仅仅是最后一个段落。

我使用的代码如下:

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 

我需要什么样的变化,使这个为表格单元格的最后一段工作?

+0

欢迎来到SO!你的第一个优秀问题!因此,如果我理解正确,如果文档中间有一个表格,并且有多个单元格,并且第一个单元格有多个段落,那么您只希望修改第一个单元格中的最后一个段落?但目前该单元格中的所有段落都受到影响? – jmstoker

+0

感谢您的欢迎和反馈。也许有些图像可以最好地描述这个问题:http://imgur.com/a/TVSBX 1.在任何宏运行之前。 2.宏已正确修改表格单元格中的第二段。 3.宏选择表格单元格中的第三段(因为它包含单元格标记的末尾,它会突出显示所有段落)。 4.格式化已应用,但适用于整个单元格。不是预期的效果。 – jarch3r

+0

由于我的网络限制,我无法查看链接,但是我创建了一个带有2列宽3行的表单文档,在单元格(2,2)中放置了两个段落,并且您的代码分别处理了两个段落。如果你打开符号'ctrl' +'shift' +'*'你在单元格中看到多个段落符号还是只有一个? – jmstoker

回答

2

“小区结束” 标志物是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 
+0

更简单的解决方案! – jmstoker

+0

感谢您提供EOC的信息。这肯定会帮助我找到解决方案,明天我会有更多时间来测试。不幸的是,我对'rng.MoveEnd wdCharacter,-1'没有任何好运,因为它似乎没有取消选择单词中的EOC字符(甚至不修改范围,只在最后一段时选择整个单元格)。类似的EOC材料:http://www.vbaexpress.com/kb/getarticle.php?kb_id=631 – jarch3r

+0

在我的测试中(使用我发布的代码)它工作得很好:当在表格单元格末尾选择一个段落时,只有这一段 - 并没有任何其他细胞 - 被选中。 –

1

该程序将首先扫描不在表格中的所有段落,然后检查所有表格,只对每个单元格中的最后一段应用更改。

CheckParagraphs

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 

结束子

CheckTables

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 
+0

我以前曾经想过把这些案例分开,但是我仍然不知道如何处理案例。感谢你们这个美好的例子,我会在早上与他们合作并进行测试。我唯一担心的是'.Paragraphs(.Paragraphs.count).Range'将仍然包含EOC字符,我还没有弄清楚如何从一个范围中排除。我对'rng.MoveEnd wdCharacter,-1'或'myRange.Collapse Direction:= wdCollapseEnd'没有任何好运,似乎也没有真正修改范围(我不确定为什么)。明天进行更多测试后我会更新。 – jarch3r

+0

再次感谢与我一起解决问题。如你所知,其他答案完美无缺。 – jarch3r

相关问题