none
How to Insert the Citation with the corresponding Bibliography using c# RRS feed

  • Question

  • Hi,

    I want to know how to insert the citation with the corresponding bibliography in the active document,I  code and tried to make what I want but no luck, I still can't insert the citation with the corresponding bibliography.

    I have code here I hope someone can help me solve my problem.

    private void InsertCitation_Click(object sender, RibbonControlEventArgs e)
            {
                // Insert a citation after the text just inserted to the bibliography
                // source added previously.
            
                Application wordApplication = Globals.ThisAddIn.Application;
                //wordApplication.Selection.EndKey(ref paramWdUnits,ref paramWdMovementType);
                wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                   ref paramWdFieldTypeCitation, dropDown1.SelectedItem.Label,//ref paramBiblioSourceTag,
                    ref paramMissing);
               // wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
    
                       // ref paramWdFieldTypeCitation,
                  //   ref paramBiblioSourceTag,ref paramMissing);
               
                
                // Insert a page break after the citation added previously and then
                // add a bibliography to the document.
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Range rng = doc.Content;
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                rng.Collapse(ref oCollapseEnd);
                wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
    
                //If find the Bibliography field in the document then update the field
                //otherwise insert a Bibliography field 
                bool isFoundFieldBibliography = false;
                foreach (Field field in wordApplication.ActiveDocument.Range().Fields)
                {
                    if (field.Type == WdFieldType.wdFieldBibliography)
                    {
                        isFoundFieldBibliography = true;
                        //update BibliographyField
                        field.Update();
                    }
                }
                if (!isFoundFieldBibliography)
                {
                    wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
    
                }
            }private void RefreshBibliography_Click(object sender, RibbonControlEventArgs e)
            {
                Refresh_Bibliography();
            }
            private void Refresh_Bibliography()
            {
                this.dropDown1.Items.Clear();
                foreach (Source source in Globals.ThisAddIn.Application.ActiveDocument.Bibliography.Sources)
                {
                    RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                    item.Label = source.Tag;
                    this.dropDown1.Items.Add(item);
                }
            }

    what happen in this code is when I press Insert Citation the Citation is being Inserted and the Bibliography , the problem is after the Citation Inserted all Bibliography is being insert too and what I want is only the corresponding bibliography of the inserted citation will be insert in the active document.

    please help me.

    Thank you in advance

    More Power

    God Bless

    Sunday, January 12, 2014 12:42 PM

All replies

  • Hi,

    I want to know how to insert the citation with the corresponding bibliography in the active document,I  code and tried to make what I want but no luck, I still can't insert the citation with the corresponding bibliography.

    I have code here I hope someone can help me solve my problem.

    private void InsertCitation_Click(object sender, RibbonControlEventArgs e) { // Insert a citation after the text just inserted to the bibliography // source added previously. Application wordApplication = Globals.ThisAddIn.Application; //wordApplication.Selection.EndKey(ref paramWdUnits,ref paramWdMovementType); wordApplication.Selection.Fields.Add(wordApplication.Selection.Range, ref paramWdFieldTypeCitation, dropDown1.SelectedItem.Label,//ref paramBiblioSourceTag, ref paramMissing); // wordApplication.Selection.Fields.Add(wordApplication.Selection.Range, // ref paramWdFieldTypeCitation, // ref paramBiblioSourceTag,ref paramMissing); // Insert a page break after the citation added previously and then // add a bibliography to the document. Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; Word.Range rng = doc.Content; object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd; rng.Collapse(ref oCollapseEnd); wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType); wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak); //If find the Bibliography field in the document then update the field //otherwise insert a Bibliography field bool isFoundFieldBibliography = false; foreach (Field field in wordApplication.ActiveDocument.Range().Fields) { if (field.Type == WdFieldType.wdFieldBibliography) { isFoundFieldBibliography = true; //update BibliographyField field.Update(); } } if (!isFoundFieldBibliography) { wordApplication.Selection.Fields.Add(wordApplication.Selection.Range, ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing); } }

    private void RefreshBibliography_Click(object sender, RibbonControlEventArgs e)
            {
                Refresh_Bibliography();
            }
            private void Refresh_Bibliography()
            {
                this.dropDown1.Items.Clear();
                foreach (Source source in Globals.ThisAddIn.Application.ActiveDocument.Bibliography.Sources)
                {
                    RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                    item.Label = source.Tag;
                    this.dropDown1.Items.Add(item);
                }
            }


    what happen in this code is when I press Insert Citation the Citation is being Inserted and the Bibliography , the problem is after the Citation Inserted all Bibliography is being insert too and what I want is only the corresponding bibliography of the inserted citation will be insert in the active document.

    please help me.

    Thank you in advance

    More Power

    God Bless

    Friday, January 10, 2014 1:24 PM
  • Hi iGnjmz,

    I'd suggest asking Word related questions in the Word for Developers forum instead. There you can get the most qualified answers.

    Friday, January 10, 2014 4:17 PM
  • Hi iGnjmz,

    According to your description, you want insert the corresponding bibliography when insert the citation like figure below:

    When we insert the bibliography, Word application will insert the all source of current list into the current doucment.

    This is a feature by designed. So currently we can’t achieve this goal. If you want to Office support the feature to insert corresponding bibliography, I suggest you to submit your feedback to Office via here.

    Best regards
    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 14, 2014 8:24 AM
    Moderator
  • Hi Sir Fei,

    Yes Sir,but I found some software that insert a citation with the corresponding Bibliography and I wonder how did they make it programmatically.

    and I mean Sir in inserting citation and bibliography is like this 

    if I insert a citation it will look like this

    (Citation 1)blablabla
    
    
    
    
    
    Bibliography1
    
    
    
    and when I insert again a citation it will look this
    
    
    
    (Citation 1)blablabla(Citation 2)
    
    
    
    
    
    
    Bibliography1
    
    Bibliography2


    that's it Sir

    I always searching for a possible code but I couldn't find it but like what I said I found something in youtube exactly to what I want and they implement it in MS Word.

    here's the link Sir : http://www.youtube.com/watch?v=HSNK1YUA7hU

    I hope I can make this so that  I will not fail.

    by the way Sir Thank you

    More Power

    God Bless


    • Edited by iGnjmz Tuesday, January 14, 2014 6:55 PM
    Tuesday, January 14, 2014 6:54 PM
  • Hi iGnjmz,

    From the video I didn’t see that the word document inserted the biblioraphy as you described. It just inserted biblioraphy when first citation was inserted. Does it insert the corresponding biblioraphy to the document when the second citation is inserted? Could you provide the whole video or any screen shot?

    The biblioraphy in the document is a field, when we insert the field into the document, there is no parameter to control which citation we wanted to insert. You can refer to link below to get detail about field:

    Fields.Add

    Best regards
    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 15, 2014 11:10 AM
    Moderator
  • Hi Sir Fei,

    Thank you once again for your response,I mean every citation has a bibliography,so I want that every time I insert a citation in the document the bibliography also of that citation will be inserted below.

    here's the whole video Sir 

    link : http://www.youtube.com/watch?v=WIvTUKEVkgg

    watch in 8:51 Sir there they choose a citation to insert then the citation was inserted including the bibliography of the inserted citation,as you can see in that video Sir when they insert a citation not all bibliography are inserted only the bibliography of the inserted citation are displayed below and I don't want those complicated one sir like "insert selected citation" I only want 1 citaiton and the correspoding bibliograpy at a time just like what happen in the video from 8:51 to 11:36.

    I hope you can still help me with this Sir so that I will not fail in my project :(

    More Power to you Sir

    God Bless you

    Thursday, January 16, 2014 7:27 AM
  • Sir Fei??
    Monday, January 20, 2014 4:08 AM
  • Hi iGnjmz,

    From the whole video you provide, I still think it is can’t insert the corresponding bibliography. In the video, it inserts the bibliography at the end of the document when the citation is inserted at the first time. And then it will just update the bibliography field. You can get the detail from the video in 11:30min. When the users select the bibliography, it will select the whole bibliography source like figure below at 12:45min:

    If so, you can refer to code below to achieve the goal:

    private void InsertCitation_Click(object sender, RibbonControlEventArgs e)
            {
                // Insert a citation after the text just inserted to the bibliography
                // source added previously.
                Application wordApplication = Globals.ThisAddIn.Application;
                //wordApplication.Selection.EndKey(ref paramWdUnits,ref paramWdMovementType);
                wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeCitation, dropDown1.SelectedItem.Label/*ref paramBiblioSourceTag*/,
                    ref paramMissing);
    
                // Insert a page break after the citation added previously and then
                // add a bibliography to the document.
                wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                
                //If find the Bibliography field in the document then update the field
                //otherwise insert a Bibliography field 
                bool isFoundFieldBibliography = false;
                foreach (Field field in wordApplication.ActiveDocument.Range().Fields)
                {
                    if (field.Type == WdFieldType.wdFieldBibliography)
                    {
                        isFoundFieldBibliography = true;
                        //update BibliographyField
                        field.Update();
                    }
                }
                if (!isFoundFieldBibliography)
                {
                    wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
                }
            
            }
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 22, 2014 1:09 AM
    Moderator
  • Hi Sir Fei,

    but Sir as you can see in the video(9:26) they have lots of Sources saved but they manager to insert only the bibliography of the citaton which they insert in the document, from 10:19 Sir that's the only thing I want where they choose a reference to insert and press the button Insert,Insert there according to what the lady said it's the default and after they click or choose a certain reference they press Insert(default) as you can see in 11:51 they have lots of sources but they manage to insert the bibliography of the citation which is inserted above the document and when they insert again a citation the bibliography will update

    insert a citation for the first time

    (citation1)

    (bibliography1)

    the second time

    (citation1)(citation2)

    (bibliography1)

    (bibliography2)

    third time

    (citation1)(citation2)(citation3)

    (bibliography1)

    (bibliography2)

    (bibliography3)

    so on and so forth

    the bibliography will keep on updating every time you insert a new citaiton in the document

    by the way Sir Fei I tried your code and since my goal is currently impossible I want this one problem of the code to be fix

    in your code Sir the first time I insert a citation it will look like this

    (citation1)

    (all bibliography)

    but the second time I insert a citation

    (citation1)

    (all bibliography)

    (citation2)

    can you fix that problem sir?I want that the second time I insert a citation and so on it will look like this

    (citation1)(citation2)

    (all bibliography)

    not like this

    (citation1)

    (all bibliography)

    (citation2)

    by the way Sir Thank you very much for you reply

    More power to you

    God Bless

    Thursday, January 23, 2014 2:12 AM