none
word add-in RRS feed

  • Question


  • I'm using word add-in from visual studio, c# language. this code works for me, but how am I able to delete a bibliography that I inserted into my word document? knowing that this is my code for the insert bibliography button

    private void BtnAddReference_Click(object sender, RibbonControlEventArgs e) { using (var frm = new FrmAddCitation()) { frm.ShowDialog(); var id = frm.SelectedId; if (id > 0) { using (var db = new MarjieiDb()) { var item = (from doc in db.ReferenceDocuments where doc.DocumentId == id select doc).FirstOrDefault(); //MessageBox.Show($"Selected document:\n[{b?.DocumentId}] {b?.Title}", @"DOCUMENT", MessageBoxButtons.OK, MessageBoxIcon.Information); if (item == null) return; var wdoc = Globals.ThisAddIn.Application.ActiveDocument; var tag = "Cit01"; var strXml = $"<b:Source xmlns:b=\"http://schemas.microsoft.com/office/word/2004/10/bibliography\">" + $"<b:Tag>{tag}</b:Tag>" + $"<b:SourceType>{item.DocumentType}</b:SourceType>" + $"<b:Author><b:Author><b:NameList><b:Person><b:Last>{item.Author}</b:Last><b:First></b:First></b:Person></b:NameList></b:Author></b:Author>" + $"<b:Title>{item.Title}</b:Title>" + $"<b:Year>{item.PublishYear}</b:Year>" + $"<b:City></b:City>" + $"<b:Publisher>{item.Publisher}</b:Publisher>" + $"</b:Source>"; wdoc.Bibliography.Sources.Add(strXml); wdoc.Fields.Add(wdoc.Range(), WdFieldType.wdFieldCitation, tag); var citation = new Citation { CitationIndex = 0,

               Style = cboStyle.SelectedItem.Label,
                            };
                            citation.WordFileId = Convert.ToInt32(db.InsertWithIdentity(citation));
         



    • Edited by mum_0101 Sunday, April 29, 2018 7:15 PM
    Sunday, April 29, 2018 7:13 PM

All replies

  • Hi mum_0101,

    For deleting the Bibliography, you need to delete the field which reference the bibliography first.

                var wdoc = Globals.ThisAddIn.Application.ActiveDocument;
                var tag = "Cit01";
                var fields = wdoc.Fields.Cast<Field>().Where(f => f.Code.Text.Contains(tag)).ToList();
                foreach (var field in fields)
                {
                    field.Delete();
                }
                
                var source = wdoc.Bibliography.Sources.Cast<Source>().Where(s => s.Tag == tag).FirstOrDefault();
                source.Delete();
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 1, 2018 6:05 AM