Word Automation: determine if a page break is needed?

I am working on a C # project that will create a Word document using the Word Automation API.

I would like to insert page breaks at certain points in the generated document, and I am currently doing this successfully with the following code:

// Generate page break
object pageBreak = WdBreakType.wdPageBreak;
wordApp.Selection.InsertBreak(ref pageBreak);

However, if the document was naturally wrapped on the next page in any case after leaving the room on the previous page, then I really do not want to generate a page break, otherwise I get a blank page.

I would really like to know where the cursor is located, and if it is in the first row and column of the current page, I can safely assume that there is no need to insert a page break.

Is there a way to access the cursor position? Or another solution that will do the same? This seems like a simple requirement, so I apologize in advance if I missed the obvious.

+5
source share
2 answers

Assuming that you programmatically create a document so that wordApp.Selection correctly reflects your actual (and corresponding) position in the document, you can define its row and column on your start page using its "Information" property and the following two WdInformation enumerations ( shown here as VBA, not sure about .NET PIA syntax):

line = wordApp.Selection.Information(wdFirstCharacterLineNumber)
col = wordApp.Selection.Information(wdFirstCharacterColumnNumber)

Ln Col, Word. 1, , .

!

+5

, . , Word Automation API , , .

0

All Articles