如何选择要删除的某个文档变量?

时间:2018-04-16 06:39:43

标签: vb.net openxml

下面的代码是删除所有文档变量。我只想删除某些文档变量。我如何设置条件?

'Remove all 
Public Sub RemoveDocVariables(ByRef fileName As Object)
    Using doc = WordprocessingDocument.Open(fileName, True)
        doc.MainDocumentPart.DocumentSettingsPart.Settings.RemoveAllChildren(Of DocumentVariables)()
    End Using
End Sub

1 个答案:

答案 0 :(得分:0)

您需要手动迭代收藏

Public Sub RemoveDocVariables(ByRef fileName As Object)
    Using doc = WordprocessingDocument.Open(fileName, True)
        For Each DV As DocumentVariables In doc.MainDocumentPart.DocumentSettingsPart.Settings.OfType(Of DocumentVariables).ToArray()
            If PlaceYourConditionHere Then doc.MainDocumentPart.DocumentSettingsPart.Settings.Remove(DV)
        Next
    End Using
End Sub