【问题标题】:Delete all text following a tagged Content control in a table cell in Word删除 Word 中表格单元格中标记的内容控件后面的所有文本
【发布时间】:2016-02-10 23:28:17
【问题描述】:

不知道如何使用 Word 对象模型 API 完成此任务。代码在大部分用 VB.Net 编写的 Microsoft Word Addin 中运行。

以下是相关任务的大纲:

给出对表格单元格的引用:

1 找到一个标记的内容控件(它将是该控件的单个实例)

2 删除该控件之后的所有文本和新行。

3 确保在该内容控件和单元格末尾之间保留(插入)一个新行。

编辑:

Sub Macro2()
  Dim endOfCell As Integer
  Dim count As Integer

  Selection.EndOf Unit:=wdCell
  endOfCell = Selection.range.End
  Do While (Selection.Information(wdInContentControl) = False)
    Selection.MoveLeft Unit:=wdCharacter
    count = count + 1
  Loop

  Selection.MoveRight Unit:=wdCharacter
  count = count - 1
  Selection.MoveRight Unit:=wdCharacter, count:=count, Extend:=wdExtend
  Selection.Delete Unit:=wdCharacter, count:=1
End Sub

但不幸的是,当集成到 VB.Net MS Word Addin 解决方案中时,此代码不起作用

【问题讨论】:

  • StackOverflow 不是代码编写服务:请向我们展示您已经尝试过的内容,并详细说明它是如何不工作的/在什么时候卡住了。由于您对网站比较陌生,因此您可能还想花点时间阅读帮助中心中有关问题预期内容的指南。
  • @CindyMeister。我大概可以在 10-15 分钟内在 Open XML SDK 或 XSLT 中完成这项任务。但到目前为止,我还没有找到一种使用对象模型 API 对其进行编码的方法。我尝试操作范围,但遇到“无法编辑范围”、“无法删除范围”错误。这就是我在这里发布问题的原因。
  • 我再说一遍:请向我们展示您的尝试。并指出触发错误的行。我的最佳猜测:您的范围包括单元格结束标记。
  • @CindyMeister。我刚刚发布了我 5 天前插入的代码。再一次,我没想到有人会为我编写代码。我正在寻找有关采取方法的建议。

标签: vb.net vba ms-word


【解决方案1】:

这是我的最终(希望是)代码。

Private Sub CleanupTableCell(range As Word.Range)
    Dim n As Integer = range.Cells.Count
    If n > 0 Then
        range = range.Cells(1).Range
    End If
    range.Select()
    Dim selection As Word.Selection = range.Document.ActiveWindow.Selection

    selection.EndOf(Unit:=Word.WdUnits.wdCell)
    selection.Bookmarks.Add("CellBookmark")
    Do While (Not SelectionInsideContentControl(selection))
        selection.MoveLeft(Unit:=Word.WdUnits.wdCharacter)
    Loop

    selection.MoveRight(Unit:=Word.WdUnits.wdCharacter, Count := 2)
    selection.Bookmarks.Add("ContentControlBookmark")
    Dim delRange As Word.Range = range.Document.Range()
    delRange.Start = range.Document.Bookmarks("ContentControlBookmark").Range.End
    delRange.End = range.Document.Bookmarks("CellBookmark").Range.Start
    delRange.Delete()
    if range.Document.Bookmarks.Exists("CellBookmark") then
        range.Document.Bookmarks("CellBookmark").Delete()
    End If
    if range.Document.Bookmarks.Exists("ContentControlBookmark") then
        range.Document.Bookmarks("ContentControlBookmark").Delete()
    End If
End Sub

这里是辅助函数的定义:

Private Function SelectionInsideContentControl(selection As Word.Selection) As Boolean


Dim controls As Word.ContentControls =   selection.Document.SelectContentControlsByTag(STR_IMergeField & "CrossComplaintPartyOneType")

    dim count as Integer = controls.Count

    if count = 0 Then
        Return False
    End If
    Dim control as Word.ContentControl = controls(count)
    If selection.InRange(control.Range) Then
        Return True
    Else 
        Return False
    End If
End Function

【讨论】:

  • 所以你是说这段代码符合你的要求?如果是,请不要忘记单击左侧的复选标记以将其标记为这样。我注意到函数 SelectionInsideContentControl 不包括在内。如果您将其包括在内,它将对可能有类似问题的其他人有所帮助。点击代码下方的“编辑”,将其添加到“答案”中。
  • 谢谢。我刚刚添加了辅助函数定义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多