Blank line after deleting contentControl in word

2020-04-23 12:30发布

问题:

I'm trying to write in a word document via VB.net and for this I'm using contentControls in my Word Document but sometimes I have to delete a contentControl or another via VB code.

It's kind of easy with contentcontrol.delete but when this contentControl contains multipleline and I want to delete it then it leaves a blank line. How can I avoid this?

回答1:

I will give you some tips based on VBA which I hope you could easily convert to vb.net and your solution.

You need to cover complete range of ContentControl including beginning and end of the object. You could do it in this way (VBA code for first CC in activedocument):

With ActiveDocument.ContentControls(1)
    'just to make a presentation- let's select range to be deleted
    ActiveDocument.Range(.Range.Start - 1, .Range.End + 2).Select
    'and we delete selection
    Selection.Delete
End With

Obviously you could combine .Select and .Delete lines into one to avoid selection in this way:

With.... and so on
    ActiveDocument.Range(.Range.Start - 1, .Range.End + 2).Delete
End with