Title a post afrter an Enumeration is strange but I think this is the best way to index this post in the search engines. This post originates from my compassion for those who, like me today, have to work with Word and the .NET Interop libraries. This enumeration is quite important, because it allows to move the selection (thus the virtual cursor we move using code) through the various “Views” composing a document.
MSDN contains this enumeration through its pages, (here) exactly in the same way it contains all the knowledge on any library, class, object or whatever developed through time by the Microsoft Developers, but knowing well the developers, (my hand rises first) and their little inclination to documentation and description of things, the description of the enumeration options is the english plain writing of their names; unfortunately not enough to answer the questions raising in the head of those who need to use it in real coding.
Therefore, I had to develop a test application to understand how this thing works and being a nice human being… (well my fellow developers call me the snike :-P) I share the informations I found in the hope to help those who need to drive data writing in a Word document through .Net code.
A word document can be generated with four different Headers and Footers types, that is:
- Different for first page.
- Different for first page and the even and odd pages.
- Different for even and odd pages.
- All the same.
To access the Headers and Footers content you need to write something like this:
public Word.Range SelectWholeViewPlace(Word.WdSeekView pSeekPlace)
{
try
{
Doc.Activate();
Word.Range rng = null;
try
{
Doc.Application.ActiveWindow.View.SeekView = pSeekPlace;
object units = Word.WdUnits.wdStory;
object extend = Word.WdMovementType.wdMove;
Doc.Application.Selection.HomeKey(ref units, ref extend);
extend = Word.WdMovementType.wdExtend;
Doc.Application.Selection.EndKey(ref units, ref extend);
rng = Doc.Application.Selection.Range;
}
catch (Exception)
{
rng = null;
}
return (rng);
}
catch (Exception ex)
{
EventLogger.SendMsg(mClassName,
System.Reflection.MethodBase.GetCurrentMethod(), ex, MessageType.Error);
throw new ApplicationException(GlobalConstants.TXT_SPACE + mClassName + GlobalConstants.TXT_DOT
+ System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
}
This method selects all content in one of the Word Document “Views”, and if the view does not exist (for example the document is in mode 4 with no Even and Odd or First page Headers and Footers returns a null Range object.
But which option do I need to use in which case? Here are the descriptions:
| Value |
Meaning |
| wdSeekCurrentPageFooter |
Activates View on the current page footer;
Current page means: the page on which the cursor is set, so if we are in Case 1 and the document is opened with cursor on first page, this option selects the First page Footer; If we are in Case 2 and we are on an odd page different from first page, this option selects the Odd Pages Footers and so on.
So this option is difficult to use in an Interop code driven editing, because we won’t be sure of where we are.
|
| wdSeekCurrentPageHeader |
Activates View on the current page header;
It has the same behaviour and the same “odds” as the previous options. |
| wdSeekEndnotes |
Activates view on the End Notes of the document if they exist. |
| wdSeekEvenPagesFooter |
Activates View on Even Pages Footer, so it works in case 2 and 3 and does not exist in case 1 and 4. |
| wdSeekEvenPagesHeader |
Activates view on EvenPages Header;
See previous row for description of behaviour. |
| wdSeekFirstPageFooter |
Activates View on First page Footer; It works in case 1 and 2 and does not exist in case 3 and 4. |
| wdSeekFirstPageHeader |
Activates View on First page Headee;
See previous row for description of behaviour. |
| wdSeekFootnotes |
Activates View on Foot Page Notes; (if they exist) |
| wdSeekMainDocument |
Activates View on Document Text. |
| wdSeekPrimaryFooter |
Activates View on Primary footer, this option as the Current page option has a different behaviour based on the Case we use.
- Activates View on footer for pages different from first one.
- Activates View on footer of Odd pages.
- Activates View on footer of Odd pages.
- Activates View on primary (and only available) footer.
|
| wdSeekPrimaryHeader |
Activates View on Primary header;
See previous row for description of behaviour; |
Have an happy Interop everybody.