简体   繁体   中英

Word VBA: After Insert a page break, My text box on page1 all go to page2

I am writing MS Word VBA code to generate word doc(the doc is purely generated by code).

At the very beginning, I placed a bunch of TextBoxes on page 1 of the document with some content in it using VBA Code ActiveDocument.Shapes.AddTextBox()... .

After finished all the textboxes task on page 1, now I want to insert a new page at the end, and begin putting more Textboxes on page 2 using VBA code

I use Selection.InsertBreak wdPageBreak at the end of my current code to get the second page. Bear in mind, currently, there is no text on page 1, only several Textboxes.

What I get from the above wdPageBreak is a new Page, but all my finished TextBoxes go to the 2nd page.

What I want is after InsertBreak, I have a blank new page2 ready to be worked on, and the finished TextBoxes stay where they were(page 1)

I have around more than 50 TextBoxes to work on. So I will need this trick again and again.

New to Word VBA Programming, very appreciate for your time to answer my stupid question.

I found a solution by myself, I post here, hopefully can help other people have similar question.

If you have no text in your page, only shapes, the shapes anchor to the page beginning character position. if you use Selection.InsertBreak to insert a new blank page, the anchor will go to the new second page.

So you can just put one line of code before you insertbreak, which is Selection.EndKey , Then everything should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM