

When building Word documents via code, you will likely need to create a new section. Notice the use of the Range object to read this information. This procedure builds a string that 1) lists each document section and 2) contains the section’s Start and End character position. Str = str & vbTab & "end = " & curDoc.Sections(i).Range. Str = str & vbTab & "start = " & curDoc.Sections(i).Range.Start This collection resides directly beneath the Document object. You can loop through document sections by accessing the Sections collection. Sections allow you to define different page layouts and header/footer schemes. The Section objectĭocuments can contain multiple sections. Within it are major items like paragraphs and sentences. The takeaway is that the Range object contains a section of the Word document. To mix it up, the final document’s text is upper case. The idea is the same… create a new document that contains the sentences in reverse order. This one is similar to paragraphs but we switch sentences for paragraphs. You can learn more about predefined bookmarks at MSDN. This is a standard Word bookmark that always exists and allows easy selection of the page containing the current focus (or selection). In this procedure, I employ the predefined “page” bookmark. Private Sub SelectedPageRange()ĬurDoc.Bookmarks( "\page").Range. The reason for this sample is that I want to point out that the Range object is darn near everywhere within Word. In this sample, we use a Range object that’s buried deeper in the Word object model. It’s a good bar trick that only works in nerd bars. This string is then inserted into a new document.
#How to move pages in word document code#
Then the code finds out how many paragraphs exist and loops backwards to create a string. The code first grabs all the document content and assigns it to a Range variable (rng). The key is the use of the Range object and all the document content. Private Sub EnumerateParagraphs()ĭim curDoc As Word.Document = WordApp.ActiveDocument This method loops through all the paragraphs in the active document and reverses their order in a new document. Just know that together, these two objects allow you to select and find Word content. I covered the selection object in this article so I will focus on the Range object.

The quickest way to achieve this goal is to master the Range and the Selection objects.
#How to move pages in word document how to#
The best way to figure out how to make things happen via code is to learn to think like Word thinks. The Word object model is extensive and more than a little complex. By the end of the samples, my hope is you will have a general idea of how to access and edit content within a Word document. I’ll start with the Range object and keep going as long as I have paper. I want to give you a lot of VB.NET code samples today. Accessing Word document content objects with code There are more “content” objects than these but the list above are the major objects that provide access to everything else. They contain ranges starting and ending character positions within the Word document. Perhaps its two most popular properties are Start and End. It contains a contiguous section of a document.
