Phil Hannent
Phil Hannent

Reputation: 12317

How do I programatically change the layout of a Word 2010 document?

We have an application that reads Word documents and imports them into our file format.

A recent bug was found that the page count is only available in Page Layout View, however Word 2010 defaults to Web Layout.

Using .NET c# how can we change the view to give us back the page count?

Upvotes: 4

Views: 3476

Answers (2)

Sateesh
Sateesh

Reputation: 1

//Here is my code snipped based on 'DocumentFormat.OpenXml' nuget package.

//Open doc file
using (var wordDocument = WordprocessingDocument.Open(strInputFile,true))
             {
                 SectionProperties sectionProps = new SectionProperties();
//Set page margins
                 PageMargin margin = new PageMargin() { Top = 1008, 
                                                            Right = (UInt32Value)1008U, 
                                                            Bottom = 1008, 
                                                            Left = (UInt32Value)1008U, 
                                                            Header = (UInt32Value)720U, 
                                                            Footer = (UInt32Value)720U, `enter code here`
                                                            Gutter = (UInt32Value)0U };
                 sectionProps.Append(margin);
                 //Apply margin
wordDocument.MainDocumentPart.Document.Body.Append(sectionProps);
//Save changes
                 wordDocument.Save();
 }

Upvotes: 0

Lazarus
Lazarus

Reputation: 43084

I believe the property you are looking for is Document.ActiveWindow.View.Type = wdPrintView; You can read more in this on MSDN.

Upvotes: 9

Related Questions