Info |
---|
We recommend that you read about How WordApplication represents a Word document Word file representation in WordApplication before reading this article. |
How WordApplication
...
inserts elements
Excerpt |
---|
Each of the elements listed in the document tree structure inherit from the Element class, which provides most of the methods for inserting new elements into a document. WordApplication will allow you to call any Insert method on almost any type of element, but the behavior of that method will change depending on what type of element the method is called on and what type of element the method is inserting |
...
For example:
Inserting a new Section automatically inserts a new Paragraph and CharacterRun within that Section.
Inserting a new Paragraph automatically inserts a new CharacterRun within that Paragraph.
To further illustrate this point, we will examine calling InsertCharacterRun on InsertTextAfter on a Section:
Example: Calling InsertCharacterRunAfter on a Section
This example shows the behavior of WordWriter when inserting an element two levels deeper than the current element.
A CharacterRun must be the child of a Paragraph, but InsertTextAfter can can be called on a Section. In this case, a new CharacterRun will be inserted at the end of the last Paragraph in the Section.
Csharpsection | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 |
| ||||||||||||||||
| 1
| 1 |
|
To achieve the most predictable behavior, it is recommended practice to call a Create or Insert method on an object appropriate to what is being inserted.
So, for example, instead of calling InsertTextAfter on on a Section object as described above, it should be called on a Paragraph, ListEntry, or field Field contents.
Csharpsection | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 |
| ||||||||||||||||||||
| 2
| 2 |
|
Here is an example Word document before running either of the code samples above:
SCREEN SHOT BEFORE CODE
SCREEN SHOT AFTER CODE
List of Insert Behaviors
The following table lists some common examples of how WordWriter behaves when a Create or Insert method is called on different elements.
Each column represents the following:
Parent element:
Element onElement on which InsertXBefore or InsertXAfter is called.
Element to be inserted:
TypeType of element which is being inserted. Use the appropriate InsertXBefore or InsertXAfter for this type.
InsertXBefore Behavior:
WhatWhat the InsertXBefore method does in this case.
InsertXAfter Behavior: What the InsertXAfter method does in this case.
Notes:
To insert a Section, use CreateSectionBefore
orTo insert a CharacterRun, use InsertTextBefore or InsertTextAfter.
Parent element Element | Element to be inserted | InsertXBefore Behavior | InsertXAfter Behavior | |
---|---|---|---|---|
Document | Section | New Section is inserted before existing Sections Sections. | New [[Section] is inserted after existing Sections Sections. | |
Document | Paragraph, List, or Table | Element is inserted in the first Section of the document Document before any existing elements in that Section. | Element is inserted in the last Section of the document Document after any existing elements in that Section. | |
Document | CharacterRun, InlineImage, Hyperlink, or MergeField | Element is inserted in the first element ( Paragraph , List , or Table ) of the first Section of the document Document before any existing elements in that first element. | Element is inserted in the last element ( Paragraph , List , or Table ) of the last Section of the document Document after any existing elements in that last element. | |
Section | ||||
Section | CharacterRun , InlineImage, Hyperlink, or MergeField | Element is inserted in the first element ( Paragraph , List , or Table ) of this Section of the Document before any existing elements in that first element. | Element is inserted in the last element ( Paragraph , List , or Table ) of this | Section afterSection of the Document before any existing elements in that last element. |
List | List | |||
List | CharacterRun , InlineImage, Hyperlink, or MergeField | Element is inserted in the first ListEntry of the List before any existing elements in that ListEntry. | Element is inserted in the last ListEntry of the List after any existing elements in that ListEntry. | |
Table | Table | Table is inserted in the first TableCell of the current Table before any existing elements in that TableCell. | There is currently an issue with this behavior. New rows are appended to the current Table instead of being added to a new Table. | |
Table | Paragraph | Paragraph is inserted in the first TableCell of the current Table before any existing elements in that TableCell. | Paragraph is inserted after the Table. | |
Table | List | List | isis inserted before the Table. There is currently an issue with this behavior. | TheList is inserted after the Table and appears as a TableCell. |
Table | CharacterRun , InlineImage, Hyperlink, or MergeField | Element is inserted in the first element (Paragraph, List, or Table) of the first TableCell of the Table before any existing elements in that first element. | Element is inserted in the next element (Paragraph, List, or Table) after the Table, after any existing elements in that next element. |