none
problem with using other type of Source RRS feed

  • Question

  • Hi,

    I have a code here that Insert a Citation and Bibliography from a listBox,now my problem is that when I save a Source using Book Section,Web Site,Document from Web site and patent in the Current List I got an Exception error.

    please try to run my code and save a Source having a type of Source "Book Section" , "Web Site","Document from Web site" or "Patent" and then press the button named "btmInsertCitation" and you'll see the Exception Error I'm talking about.

    by the way here's the code

    private void InsertCitationForm_Load(object sender, EventArgs e)
            {
                BindCurrentList();
            }
            private void BindCurrentList()
            {
    
                Word.Application App = Globals.ThisAddIn.Application;
                Word.Document doc = App.ActiveDocument;
                Word.Sources sour = doc.Bibliography.Sources;
    
                    string[] strItems = new string[sour.Count];
                    for (int i = 1; i <= sour.Count; i++)
                    {
                        Word.Source ssour = sour[i];
                        string tag = ssour.Tag;
    
    
                        strItems[i - 1] = ssour.Field["Author"] + "; " + ssour.Field["Title"] + " (" + ssour.Field["Year"] + ")";
    
                        listBox1.DisplayMember = "Name";
                        listBox1.ValueMember = "Tag";
                        listBox1.Items.Add(new Test(strItems[i - 1], tag));
                    }
    
                
    
    
                
             
            }
    
            
    
            public class Test
            {
                public string Name { get; set; }
                public string Tag { get; set; }
                public Test(string name, string tag)
                {
                    Name = name;
                    Tag = tag;
                }
            }
    
            private void btmInsertCitation_Click(object sender, EventArgs e)
            {
                Test test1 = listBox1.SelectedItem as Test;
    
                Microsoft.Office.Interop.Word.Application wordApplication = Globals.ThisAddIn.Application;
    
                wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeCitation, test1.Tag,
                    ref paramMissing);
    
                wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
    
                bool isFoundFieldBibliography = false;
                foreach (Field field in wordApplication.ActiveDocument.Range().Fields)
                {
                    if (field.Type == WdFieldType.wdFieldBibliography)
                    {
                        isFoundFieldBibliography = true;
    
                        field.Update();
                    }
                }
                if (!isFoundFieldBibliography)
                {
                    wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
                }
                wordApplication.Selection.PreviousField().Select();
                wordApplication.Selection.PreviousField().Select();
                wordApplication.Selection.MoveRight(WdUnits.wdCharacter, 1);
                this.Close();
               
            }

    I hope someone could fix this

    More Power

    God Bless us all.


    • Edited by iGnjmz Thursday, January 30, 2014 2:03 PM
    Thursday, January 30, 2014 1:52 PM

All replies

  • Hi iGnjmz,

    Which exception did you get? I put the code you provided in a user control and create a custom task pane to contain the user control. I got the exception when I started the project, because there is no active document when the Word application load the add-in.

    Then I inserted the both Book section and web site section successfully. Do I miss anything?

    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.

    Friday, January 31, 2014 6:06 AM
    Moderator
  • Hi,

    I have a code here that Insert a Citation and Bibliography from a listBox,now my problem is that when I save a Source using Book Section,Web Site,Document from Web site and patent in the Current List I got an Exception error.

    please try to run my code and save a Source having a type of Source "Book Section" , "Web Site","Document from Web site" or "Patent" and then press the button named "btmInsertCitation" and you'll see the Exception Error I'm talking about.

    by the way here's the code

    private void InsertCitationForm_Load(object sender, EventArgs e)
            {
                BindCurrentList();
            }
            private void BindCurrentList()
            {
    
                Word.Application App = Globals.ThisAddIn.Application;
                Word.Document doc = App.ActiveDocument;
                Word.Sources sour = doc.Bibliography.Sources;
    
                    string[] strItems = new string[sour.Count];
                    for (int i = 1; i <= sour.Count; i++)
                    {
                        Word.Source ssour = sour[i];
                        string tag = ssour.Tag;
    
    
                        strItems[i - 1] = ssour.Field["Author"] + "; " + ssour.Field["Title"] + " (" + ssour.Field["Year"] + ")";
    
                        listBox1.DisplayMember = "Name";
                        listBox1.ValueMember = "Tag";
                        listBox1.Items.Add(new Test(strItems[i - 1], tag));
                    }
    
                
    
    
                
             
            }
    
            
    
            public class Test
            {
                public string Name { get; set; }
                public string Tag { get; set; }
                public Test(string name, string tag)
                {
                    Name = name;
                    Tag = tag;
                }
            }
    
            private void btmInsertCitation_Click(object sender, EventArgs e)
            {
                Test test1 = listBox1.SelectedItem as Test;
    
                Microsoft.Office.Interop.Word.Application wordApplication = Globals.ThisAddIn.Application;
    
                wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeCitation, test1.Tag,
                    ref paramMissing);
    
                wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
                wordApplication.Selection.InsertBreak(WdBreakType.wdLineBreak);
    
                bool isFoundFieldBibliography = false;
                foreach (Field field in wordApplication.ActiveDocument.Range().Fields)
                {
                    if (field.Type == WdFieldType.wdFieldBibliography)
                    {
                        isFoundFieldBibliography = true;
    
                        field.Update();
                    }
                }
                if (!isFoundFieldBibliography)
                {
                    wordApplication.Selection.Fields.Add(wordApplication.Selection.Range,
                    ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing);
                }
                wordApplication.Selection.PreviousField().Select();
                wordApplication.Selection.PreviousField().Select();
                wordApplication.Selection.MoveRight(WdUnits.wdCharacter, 1);
                this.Close();
               
            }

    I hope someone could fix this

    More Power

    God Bless us all.

    Saturday, February 1, 2014 11:19 AM
  • Hi Sir Fei,

    In my case Sir I made a Ribbon(Visual Designer) which has a button that show the  Windows Form,and in the Windows Form I put a listbox(listBox1) and 1 button(btmInsertCitation) then put the code stated above. After that I go to Reference Tab and click "Manage Source" then add a new Source having different type of Source and I notice that if I add a new Source having this type of Source "Book Section" "Web Site" "Document from  Web site" and "Patent" or if the Current List have an Item having this kind of Sources will cause an Exception Error if I click the button in the Ribbon(Visual Design).

    I hope you can help me with this Sir Fei.

    Saturday, February 1, 2014 11:21 AM