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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,286 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,513 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 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