删除内容控件所在的行

时间:2016-02-24 15:11:24

标签: vba ms-word word-vba

我想删除内容控件所在的行。

目前该文件有以下文字:

t1
t2


Status:     Draft
Valid from: 01.01.2012
Valid for:  All Emplyoees

Created by:     a
Released by:    <content control goes here>.

我有一个变量controlff,它是对控件的引用。无论我如何得到它的范围,我总是删除全文。

这其他问题对我没什么帮助:

Blank line after deleting contentControl in word

How to remove the entire line of a word doc using vba

deleting certain lines in ms word 2007

如何删除按行发布的内容?

修改

我之前展示的所有文字都在桌子上的一行内。

1 个答案:

答案 0 :(得分:1)

以下是如何在表格中执行此操作的小示例。首先获取内容控制,将选择内容扩展到表格单元格中的上一行并删除选择。

Sub test()

    Dim rng As Range

    'Get your Content Control here based on your variable reference
    Set rng = ActiveDocument.ContentControls(1).Range

    rng.Select

    'Move outside the Content Control
    Selection.MoveRight wdCharacter
    Selection.MoveRight wdCharacter

    'Select Line Up
    Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend

    'Include the paragraph from previous line
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend

    'And ... Delete the selected area
    Selection.Delete
End Sub

希望有所帮助