开发者

How can you remove a field from a word document?

开发者 https://www.devze.com 2023-01-04 06:06 出处:网络
I\'m working on a project where the user can insert data into a document using fields, document properties and variables. The user also needs to be able to remove the data from the document. So far, I

I'm working on a project where the user can insert data into a document using fields, document properties and variables. The user also needs to be able to remove the data from the document. So far, I've managed to remove the document property and variable, but I'm not sure how 开发者_如何学PythonI would go about removing the field (that's already inserted into the document). Note that I need to compare the field to a string, and if it matches; delete it from the doc.


I'm assuming you're using .NET Interop with Word. In that case, I believe you're looking for Field.Delete.

This is of course also assuming you know how to get the field you're looking for, which would usually be enumerating through _Document.Fields (or a more finite range if you know one) until you get the right one.


The Field has a Delete method. See the documentation for Field.Delete.

So I think something like this would work:

foreach(Field f in ActiveDocument.Fields)
{
    f.Select();
    if(f.Type == TypeYouWantToDelete)
    {
        d.Delete();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消