Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to add citation in my word document using c# vsto plugin. after a long research i found a code on here :


<pre>private void AddBibliography()
{
    string guid = System.Guid.NewGuid().ToString();
    string src = 
        "<b:Source><b:Tag>Jam08</b:Tag><b:SourceType>Book</b:SourceType>" 
        + "<b:Guid>" + guid + "</b:Guid><b:LCID>0</b:LCID><b:Author>" 
        + "<b:Author><b:NameList><b:Person><b:Last>Persse</b:Last>" 
        + "<b:First>James</b:First></b:Person></b:NameList></b:Author>" 
        + "</b:Author><b:Title>Hollywood Secrets of Project Management "
        + "Success</b:Title><b:Year>2008</b:Year><b:City>Redmond</b:City>"
        + "<b:Publisher>Microsoft Press</b:Publisher></b:Source>";
    this.Bibliography.Sources.Add(src);

    this.Bibliography.BibliographyStyle = "APA";
    this.Paragraphs.Last.Range.InsertParagraphAfter();
    object fieldType = Word.WdFieldType.wdFieldBibliography;
    this.Fields.Add(
        this.Paragraphs.Last.Range, 
        ref fieldType, 
        ref missing, 
        ref missing);
}


but issue is it's not working.

What I have tried:

I have tried above code i found on DocumentBase.Bibliography Property (Microsoft.Office.Tools.Word) | Microsoft Learn[^]
Posted

The best way to understand the code required to do it is to use the macro recording feature in Word, then look at the VBA code it generates. ref: Create or run a macro - Microsoft Support[^]
 
Share this answer
 
I suspect that you might be able to use something like this:
C#
public void CreateBibliography(string pageHeading, List<Citation> citations)
{
  var document = Application.ActiveDocument;
  Range range = document.Range;
  range.Collapse(WdCollapseDirection.wdCollapseEnd);
  AddPage(range, pageHeading);
  foreach (Citation citation in citations)
  {
    range.InsertAfter($"Citation {citation.Author} - {citation.Title}: Press {citation.Press} ");
    range.InsertParagraphAfter();
  }
}

private void AddPage(Range range, string pageHeading)
{
  range.InsertBreak(WdBreakType.wdPageBreak);
  range.InsertAfter(pageHeading);
  range.InsertParagraphAfter();
}
I've just typed this up in Notepad so it might not be 100% correct, but it looks like it might work. Note, the Citation class looks like this:
C#
public class Citation
{
  public string Author { get; set; }
  public string Title { get; set; }
  public string Press { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900