无法使用Word对象模型API弄清楚如何完成此任务。代码在Microsoft Word Addin中运行,主要在VB.Net中编写。
以下是相关任务的大纲:
给出对表格单元格的引用:
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解决方案中时,此代码无法正常工作
答案 0 :(得分:2)
这是我的最终(希望)代码。
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