none
Microsoft Word add-ins RRS feed

  • Question

  • I want to make an MS word add-ins similar to the MS word Reference Menu and  I want that everytime the user insert a citation the bibliography that correspond to the inserted citation will be automatically be inserted below but I don't know where to start.

    any suggestion for my problem? or any website that I can refer to start.
    Sunday, December 8, 2013 1:58 AM

All replies

  • Hi,

    According to your description, you want to the bibliography that correspond to the inserted citation will be automatically be inserted below when users insert a citation.
    I found an article about how to insert a bibliography citation.
    Inserting a Bibliography Citation in Word 2007
    I test the code in the above article in Visual Studio 2012 and Office 2013, I modify the code a bit to run it like below:
    Before changing.

    ApplicationClass wordApplication = null;
    wordApplication = new ApplicationClass();
    
    After changing
    
    Application wordApplication = null;
    wordApplication = new Application();
    

    Then it insert the citation and bibliography successfully. There are two ways to insert the citation into Word document. First, the word insert the citation as default when you add a new source. Second, insert the existing source.

    To achieve your goal to insert the citation at the first way, you can create a winform like figure below to collect the citation information and create a button. In the click event of the button, you can insert the citation and bibliography.

    And in the second scenario, I suggest you create a dropdown to show all the source in the active document. And use a button to refresh the source of bibliography. Then add another to insert the citation and bibliography. You can refer to the code below to refresh and insert the citation:

     object paramMissing = Type.Missing;
            object paramWdUnits = WdUnits.wdStory;
            object paramWdMovementType = WdMovementType.wdMove;
     
            object paramWdFieldTypeCitation = WdFieldType.wdFieldCitation;
            object paramWdFieldTypeBiblio = WdFieldType.wdFieldBibliography;
    
      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);
                }
            }
      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);            wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
            }
    
            private void RefreshBibliography_Click(object sender, RibbonControlEventArgs e)
            {
                Refresh_Bibliography();
            }
    

    The ribbon like figure below:

    The result like figure below:

    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.

    • Marked as answer by iGnjmz Wednesday, December 11, 2013 3:38 AM
    • Unmarked as answer by iGnjmz Wednesday, December 11, 2013 7:55 AM
    • Marked as answer by iGnjmz Wednesday, December 11, 2013 7:55 AM
    • Unmarked as answer by Cindy Meister MVPModerator Monday, December 16, 2013 4:25 PM
    Tuesday, December 10, 2013 8:13 AM
    Moderator
  • Hi Sir Fei,

    Thank you very much for your help Sir,you're a life saver.
    I will try my best to do what you've answer to my problem but since I'm a newbie in programming if I got problems or confusions can you help me Sir?
    I just post it here and I hope you'll notice this reply.

    and by the way sir I'm using Visual Studio 2008 and MS Word Office 2007.
    More power to you Sir

    God bless.



    • Edited by iGnjmz Wednesday, December 11, 2013 2:25 AM
    Wednesday, December 11, 2013 2:09 AM
  • Hi sir Fei,

    I'm having an error and I use Windows Form Application c# Sir

    I cant put a picture sir so I uploaded the picture in my fb account to show you the error.

    link :https://www.facebook.com/photo.php?fbid=620903057945286&set=a.375385382497056.76229.100000767919724&type=1&theater


    I'm really sorry sir but I really need your help and your the only person who made a reply to my thread in other forum they just close my thread that's why I'm so thankful that someone like you made an effort about my problem.

    while waiting for your reply Sir I will still work with the code you gave.

    once again Sir Thank you
    God bless and More Power to you!




    • Edited by iGnjmz Wednesday, December 11, 2013 4:19 AM
    Wednesday, December 11, 2013 3:52 AM
  • Update Sir Fei

    I try only the Insert Citation for now and I got 1 error
    here's the link for the picture Sir : https://www.facebook.com/photo.php?fbid=621093274592931&set=a.375385382497056.76229.100000767919724&type=1&theater

    and the code Sir

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;

    namespace trial
    {
        public partial class Form1 : Form
        {
            Application wordApplication = null;
            Document wordDocument = null;

            object paramMissing = Type.Missing;
            object paramWdUnits = WdUnits.wdStory;
            object paramWdMovementType = WdMovementType.wdMove;

            object paramWdFieldTypeCitation = WdFieldType.wdFieldCitation;
            object paramWdFieldTypeBiblio = WdFieldType.wdFieldBibliography;


            public Form1()
            {
                InitializeComponent();
            }

            private void InsertCitation_Click(object sender, EventArgs 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); wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
          ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
            }

           
        }
    }

    The Error :

    Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Microsoft.Office.Interop.Word.Application' D:\ms add-ins\trial\trial\Form1.cs 15 9 trial

    I hope you will still help me with this Sir Fei

    Thank you in advance

    God bless and more power Sir.



    • Edited by iGnjmz Wednesday, December 11, 2013 1:06 PM
    Wednesday, December 11, 2013 1:05 PM
  • Update Sir Fei,

    I successfully run the program you made Sir but I notice that when I choose a citation and the time when I press the insert Citation the citation will be inserted in Document including all the Bibliography try to check the image Sir Fei if it is okay for you

    link : https://www.facebook.com/photo.php?fbid=621349334567325&set=pcb.621349367900655&type=1&theater

    I just want that only the bibliography of the inserted citation will be displayed below

    Thank you in advance Sir Fei

    • Edited by iGnjmz Thursday, December 12, 2013 3:53 AM
    Thursday, December 12, 2013 3:53 AM