Search code examples
c#ms-wordoffice-interop

C# Interop: How to iterate through citations in Word doc?


I'd like to iterate through all the citations in a Word document that I'm adding using the ribbon's "Citations & Bibliography >> Insert Citation" but am not finding where they're located. E.g., to iterate through Fields I

foreach (Field f in document.Fields)

but a citation isn't a field and there's not Document.Citations object?


Solution

  • I have zero experience with the Interop library, but based on your comments and some googling I think you need to check the Type of the Field object. The enum is called WdFieldType, and the specific value is WdFieldType.wdFieldCitation which according to the docs indicates a "Citation field". You can employ some basic filtering using LINQ:

    foreach(Field f in document.Fields.Where(x => x.Type == WdFieldType.wdFieldCitation))
    {
        // Do something with citation fields
    }