Adding Bibliography In Word 2007

Download the Code Sample

To show how to programmatically insert bibliography citations in Word 2007, this section walks through five key steps:

  1. Creating a Windows Console Application project in Microsoft Visual Studio 2005.

  2. Adding a reference to the Word 12.0 Object Library.

  3. Importing the Word interop assembly namespace.

  4. Declaring the appropriate variables.

  5. Implementing the bibliography citation insertion code.

Create a Windows Console Application in Visual Studio 2005

The first step is to create a Windows console application to show how to insert bibliography citations. However, the following code is not specific to a Windows console application. You could use it in a variety of application types.

To create a Windows Console Application project in Visual Studio 2005

  1. Start Visual Studio.

  2. On the File menu, select NewProject. The New Project dialog box appears.

  3. In the Project Types pane, select Visual C# or Visual Basic, and then select the Windows category.

  4. In the Templates pane, select Console Application.

  5. Specify a name for the project.

  6. Specify a location for the project and click OK.

    Visual Studio generates a Windows Console Application project with a single source file in it called Program.cs or Module1.vb, depending on the language selected previously in Step 3.

Add a Reference to the Word 12.0 Object Library

You must add a reference to the Microsoft Word 12.0 Object Library to the Visual Studio project in order to program against the Word object model.

To add the reference

  1. On the Project menu, select the Add Reference menu item. The Add Reference dialog box is displayed as shown in Figure 1.

  2. Click the COM tab in the Add Reference dialog box, then locate and select the Microsoft Word 12.0 Object Library component.

  3. Click OK to add the reference.

Figure 1. Adding a reference


Import the Word Interop Namespace

Import the Microsoft.Office.Interop.Word namespace to use the objects defined in the namespace without having to specify the fully qualified namespace path. To import the namespace, open the Program.cs or Module1.vb source file and add the following line to the top of the source file. For a Microsoft Visual Basic project, add the code to the very top of the source file, above the Module statement. For a Microsoft Visual C# project, add the code before the namespace statement and right after the default using statement created by Visual Studio.

using Microsoft.Office.Interop.Word;

Declare Appropriate Variables

For a Visual Basic project, place all of the following variable declarations within Sub Main(). For a Visual C# project, place all of the following variable declarations within the Main() function.

Declare the following variables to hold references to the Word objects used in the bibliography citation insertion code.

Now, add the following code to declare variables for the parameters passed to methods in the bibliography citation insertion code. You can use the paramDocumentPath variable to specify the path and file name of the Word document to be created. Replace the Path placeholder in the paramDocumentPath variable with a path to an appropriate folder.

The paramMissing variable is used to call methods that accept optional parameters.

Note

Optional parameters are optional only when using Visual Basic. You must specify a value for optional parameters when using Visual C#. If you use Type.Missing as the value for an optional parameter, this alerts the method being called that you did not specify the parameter and that the method should use the parameter's default value.

The remaining variables declared in the Visual C# code example are used to call the methods on Word objects. Many of the methods in the Word object model accept parameters of type object passed by reference. For methods called by the citation insertion code, most of the parameter values are enumerated type values which cannot be passed by reference in Visual C#. By declaring the variables for the parameters as objects and initializing them to a value from the enumerated type, it is possible to pass the value by reference. The paramWdUnits and paramWdMovementType variables are used with the Selection.EndKey method when positioning the insertion point in the document. The paramWdBreakType variable is used when inserting a page break in the document. The paramWdFieldTypeCitation, paramWdFieldTypeBiblio, and paramBiblioSourceTag variables are used when inserting a bibliography source, citation, and bibliography in the document.

ApplicationClass wordApplication = null; Document wordDocument = null;
object paramDocumentPath = @"Path\Biblio Citation.docx"; object paramMissing = Type.Missing; object paramWdUnits = WdUnits.wdStory; object paramWdMovementType = WdMovementType.wdMove; object paramWdBreakType = WdBreakType.wdPageBreak; object paramWdFieldTypeCitation = WdFieldType.wdFieldCitation; object paramWdFieldTypeBiblio = WdFieldType.wdFieldBibliography; object paramBiblioSourceTag = "Mor01";

Implement the Bibliography Citation Insertion Code

Next, add code that inserts a bibliography citation into a Word document, as shown in the following code:

  1. Starts Word and creates a document.

  2. Inserts a bibliography source into the document.

  3. Inserts some text into the document.

  4. Inserts a citation to the bibliography source added previously.

  5. Inserts a page break after the citation, and then adds a bibliography to the document.

  6. Saves the document and closes Word.

The shutdown related code closes the Word document, quits the Word application, releases references to the underlying Word COM objects, and makes calls to the .NET garbage collector. For more information about releasing COM objects when using managed code, see Chapter 2: Basics of Office Interoperability (Part 2 of 3) from the book, Microsoft .NET Development for Microsoft Office.

try { // Start an instance of Word. wordApplication = new ApplicationClass(); // Create the new document. wordDocument = wordApplication.Documents.Add(ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); // Insert a bibliography source into the document.string strXml = @"<b:Source xmlns:b=""http://schemas.microsoft.com/" + @"office/word/2004/10/bibliography""><b:Tag>Mor01</b:Tag>" + "<b:SourceType>Book</b:SourceType><b:Author><b:Author>" + "<b:NameList><b:Person><b:Last>Hezi</b:Last>" + "<b:First>Mor</b:First></b:Person></b:NameList></b:Author>" + "</b:Author><b:Title>The New Office</b:Title>" + "<b:Year>2006</b:Year><b:City>Seattle</b:City>" + "<b:Publisher>Adventure Works Press</b:Publisher>" + "</b:Source>"; wordDocument.Bibliography.Sources.Add(strXml); // Insert some text into the document. wordDocument.Content.InsertAfter( "Here is a reference needing a citation."); // Insert a citation after the text just inserted to the bibliography// source added previously. wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType); 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. wordApplication.Selection.EndKey(ref paramWdUnits, ref paramWdMovementType); wordApplication.Selection.InsertBreak(ref paramWdBreakType); wordApplication.Selection.Fields.Add(wordApplication.Selection.Range, ref paramWdFieldTypeBiblio, ref paramMissing, ref paramMissing); // Save the document. wordDocument.SaveAs(ref paramDocumentPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); } catch (Exception ex) { // Respond to the error. } finally { // Close and release the Document object.if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } // Quit Word and release the ApplicationClass object.if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); }

Using Microsoft Word to Create References and Bibliographies

Microsoft Office Word has a feature that inserts citations into your paper and formats your bibliography. This feature is not a full-fledged bibliographic management system like Zotero, but it will allow you to store bibliographic information, use that information to create in-text citations (or footnotes or endnotes) in your paper, and create a bibliography in a variety of styles.

Please note:

This option is available only in Microsoft Word 2007 and above. Previous versions of Word do not offer it.

Any errors that you make in entering the bibliographic information will be passed into your citations. Watch your spelling and punctuation, and make sure that you have entered the right information in the right places.

References

These features are located under the References tab in Microsoft Word 2007:

When you use material in a paper that should be cited, click on the Insert Citation button:

Clicking on this button brings up a menu that includes the option of entering information about a source you haven't cited in your paper yet::

Clicking Add New Source will bring up a dialog box where you can enter appropriate information about your source:

Make sure that you select the right Type of source for the source that you're working with, since the fields to be filled in will be different, depending on the kind of source.

When you've finished entering your source information, click OK to save the information about the source. This information can now be used to create in-text citations, bibliographic footnotes and endnotes, and bibliographies and works cited pages.

Creating an in-text citation

You can insert citations as you compose your paper or after you're done writing--the procedure is the same.

Place your cursor in the text of your paper where you want your citation to appear. Then click Insert Citation. A list of all of the sources that you've entered will appear:

Click on the source to insert a citation to that source in the text of the paper:

Some citation styles require that you include a page number, and there may be other things you want to change about this citation. If you mouse over the citation, you will see that it is "live":

Use the dropdown menu to edit the citation:

Note that you can also use the Edit Citation dialog box to suppress the inclusion of the author, the year of publication or the title of the work, if they have appeared in your citation and are unwelcome. When you click OK, your citation will be modified accordingly:

Managing citations

Word keeps all the information about your sources that you've entered, a handy feature if you write several papers that cite some of the same sources. If you're starting a new paper that will use some of the same sources, click on the Manage Sources button:

You will be presented with a list of all the sources whose information you have entered:

You can then copy sources from the Master list to the Current list.

These sources will now be available for you to cite in the new paper.

Creating a bibliography or works cited page

If you have all the information about all of your sources entered, you can create a bibliography or works cited page at the end of your paper. You can also create a standalone bibliography. Simply place your cursor where you want the bibliography or works cited page to begin, and click the Bibliography button:

Mouse over your preference (Bibliography or Works Cited) and click Insert Bibliography. Your bibliography will appear:

Please note that you may have to change the format of the title of the works cited page or bibliography; Microsoft does not use a standard heading for these headings.A true works cited page lists only the items you have actually cited. In order to make changes in your bibliography or works cited, you will have to convert your works cited to static text and then edit them. To convert your formatted text to static text, click on the icon at the top left of your bibliography:

Select the option you want.

Changing the style

What if you think that you have finished your paper, only to look at your professor's instructions for format and discover that you have completed your paper in the wrong style? Click on the dropdown menu that lists the possible styles you can use:

Select the style that your professor requires, and click. Your entire paper--citations and bibliography--will be reformatted in the chosen style.


You can contact the Reference Desk through the live chat service or by phone at 227-2294.

 

0 thoughts on “Adding Bibliography In Word 2007

Leave a Reply

Your email address will not be published. Required fields are marked *