Download the Code Sample
To show how to programmatically insert bibliography citations in Word 2007, this section walks through five key steps:
Creating a Windows Console Application project in Microsoft Visual Studio 2005.
Adding a reference to the Word 12.0 Object Library.
Importing the Word interop assembly namespace.
Declaring the appropriate variables.
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
Start Visual Studio.
On the File menu, select NewProject. The New Project dialog box appears.
In the Project Types pane, select Visual C# or Visual Basic, and then select the Windows category.
In the Templates pane, select Console Application.
Specify a name for the project.
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
On the Project menu, select the Add Reference menu item. The Add Reference dialog box is displayed as shown in Figure 1.
Click the COM tab in the Add Reference dialog box, then locate and select the Microsoft Word 12.0 Object Library component.
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.
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.
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.
Implement the Bibliography Citation Insertion Code
Next, add code that inserts a bibliography citation into a Word document, as shown in the following code:
Starts Word and creates a document.
Inserts a bibliography source into the document.
Inserts some text into the document.
Inserts a citation to the bibliography source added previously.
Inserts a page break after the citation, and then adds a bibliography to the document.
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.
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.
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.
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:
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.