It's like placing the cursor on that specific bookmark location on the document. Now the selection object represents that specific location. In the previous range example, we locate a bookmark and set the range on that specific bookmark and we select it. We programmatically represent the same by using the Selection Object derived from Word.Selection. The contents can be text, formatted text, tables or any other item in the document. While working on word, we select a range of text by clicking and dragging the mouse pointer across contents in the document to select it. Word.Range wrdRange = _Item(ref oBookMarkName).Range.Select() Object oBookMarkName = "My_Inserted_Bookmark_On_Template" There are also many other possible ways to set the range. In this approach, we insert Bookmarks on the base template and we programmatically locate those Bookmarks, set the range on them and insert text or documents at that specific location. I had extensively used the Bookmarks locators as I work on Automation using a base template. There are many possible ways to point to a specific location on a document. The range variable of the Word.Document object represents the location of the cursor on the current document. In order to do the same task, we use the Range variable in C#. While we work on Word Application, if we want to type some text in the 11th line, then we manually take the cursor and click it on the required line and then start typing. So instead of supplying the same input parameter ten times, we can make all the input parameters point to the location same single variable in them memory. In automation it makes sense as to why most of the input parameters are of reference types it might be because of the fact that most of the methods a multitude of input parameters (many have more than 10 input parameters) and their value is going to be either true, false or missing in most of the cases. Most of the methods have input parameters which are of reference type, and the values are mostly true, false or missing (null).
Word.Document oWordDoc = new Word.Document() Word.Application oWord = new Word.Application() If we need to add a new document file, first we have to create an instance of the Word.Document object and then add it to the Word.Application.
Creating a new instance of Word.Application can be visualized as below. This is like the base class which is needed to create a new document. This represents in Word Application without any new document loaded in it. The Word.Application and Word.Document are used to Open Word and add a new Document to it. The same steps needs to be done programmatically. (Opening Word Application creates a new document by default, but in Automation, wee need to manually add a document) Let's consider that we want to create a document using the Word Application, we might end up doing the following steps, Note: This DLL would be present only if Microsoft Office is installed on the Machine.Īlso include "using Microsoft.Office " in the Namespaces used.Īll the methods used Word automation is derived either from Word.Application or Word.Document class. This DLL has all the methods which we do to perform the automation. This can be done by right clicking the Reference Folder in the Solution explorer of the project and select Add Reference.īrowse Through the available COM objects and Select Microsoft Office 11.0 Object Library & Microsoft Word 11.0 Object Library. Starting off, the first step is to include the Word DLL's to the Solution. Tasks like Inserting Table of Contents, Linking documents, Mail Merge, Inserting Documents, Embedding documents, inserting pictures, watermark. Almost all of the tasks which we perform on word 2003 can be done programmatically using C# or VB. Word automation almost completely involves working with objects and reference types. Working on Word is considered to be straightforward, but doing the same programmatically gets a little intricate. Word Automation through C# is all about programmatically generating the Word Document using C# code.