How to read and manipulate citations in a word document using VSTO add-in?

Talha Mehboob 21 Reputation points
2021-08-21T12:37:29.313+00:00

Hi, I am trying to build a word add-in using VSTO, I need to figure out a way and read citations in my word document in my c# code (Specifically the month and year of a citation) in order to do my further processing on it.

I'm new to VSTO, so any help regarding reading and manipulating citations will be really appreciated.

Thank you.

Microsoft 365 and Office | Development | Other
Developer technologies | Visual Basic for Applications
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-08-23T03:20:05.05+00:00

    We can use WdFieldType Enum to find Citation.

    I did not use VSTO but used a console app to write a piece of code, but you should be able to easily modify it to what you need.

            Application application = null;  
            Document document = null;  
            try  
            {  
                application = new Application();  
                document = application.Documents.Open(@"C:\...\1.docx");  
    
                string str = "test";  
                foreach (Field field in document.Fields)  
                {  
                    if (field.Type == WdFieldType.wdFieldCitation)  
                    {  
                        Range range = field.Result;  
                        if (range.Text == str)  
                        {  
                            range.Underline = WdUnderline.wdUnderlineSingle;  
                        }   
                       
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine(ex.Message);  
            }  
            finally  
            {  
                document.Save();  
                document.Close();  
                application.Quit();  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.