question

TalhaMehboob-0310 avatar image
0 Votes"
TalhaMehboob-0310 asked TalhaMehboob-0310 commented

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

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.

dotnet-csharpoffice-vba-devoffice-vsto-com-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TalhaMehboob-0310 commented

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your quick response I really appreciate it.

i was able to read the citations using this. Can you please tell me a method with which i can compare the quoted citation with a declared string in my code and then underline the citation as well.

I could not find any sub method of fields object to help me with this.

0 Votes 0 ·

@TalhaMehboob-0310
I modified the code, please check if it can meet your needs now.

0 Votes 0 ·

tried this similar approach but unfortunately it does not work.

For example I have a citation added in APA style as (Name, 2016). When i run the above code and add "2016" in "str" string it does not underline the citations with the same year.

Am i missing something here?

0 Votes 0 ·
Show more comments