How can I duplicate a page in word using VBA?

If I have a page, full of shapes, linked pictures, text boxes etc, how can I create a duplicate of said page right after the first?

I am tired of copying and pasting the objects in question, especially because word deems it necessary to offset the objects (down and to the right), meaning I have to go in to the format object menu and reposition them each time.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

William ElliottConnect With a Mentor Sr Tech GuruCommented:
who is your hero? :);EN-US;Q216201

Sub BreakOnPage()
   ' Used to set criteria for moving through the document by page.
   Application.Browser.Target = wdBrowsePage
   For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
      'Select and copy the text to the clipboard
      ' Open new document to paste the content of the clipboard into.
      ' Removes the break that is copied at the end of the page, if any.
      ChangeFileOpenDirectory "C:\"
      DocNum = DocNum + 1
      ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
      ' Move the selection to the next page  in the document
   Next i
   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
William ElliottSr Tech GuruCommented:
not quite sure if there is an answer to this question . it seems the problem you have lies in the formatting. i would utilize tables to constrain the formating of your images, then cut and paste the tables
DsastrayAuthor Commented:
So, do you feel like a layout as seen in the file

would be something that could be put into tables?

Note, the text boxes have a variety of fonts, text sizes, linked fields, lower left is a linked excel table.

My sense is
a) not really
b) I try to avoid working with word tables, because I find the interface counterintuitive
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

William ElliottSr Tech GuruCommented:
i don't suppose it will allow you to export to html, then duplicate the html for the page?
William ElliottSr Tech GuruCommented:
i still say tables are the way to go. if nothing else just encapsulate everything you want into one table and copy the table. everything niside should stay intact
William ElliottSr Tech GuruCommented:
an example 

to answer your original question given that all objects carry a different name within MS word,  i personally cannot think of a way to arbitrarily copy a specific page, because assoon as you make any changed to the page, the beginning and end segments change.

possibl if you have a specific begginning and ending identifier, then you could copy the information, but i'm faily confodent that it will do the same thing as the copy and past buttons... nonetheless i'll try to work something up for that.
Why don't you upload the page and those around it in Word format? That way we can try it and discuss what happens with you.
DsastrayAuthor Commented:
Apologies for taking so long. I felt it was necessary to generate a better generic file before I posted the word document:

One of the possible solutions I am thinking of is along the lines of:
Make a copy of the file
Delete all non relevant pages
Save copy
Insert the copied file into the original at the desired position.

I am not as familiar as I would like to be regarding Weelio's reference to the beginning and end segments. I know that I have successfully written code in the past that allowed me to delete a single page based on a bookmark on that page.

BookMarkName = rec("BookMark")
If wordDoc.Bookmarks.Exists(BookMarkName) = True Then
      wordApp.Selection.Goto What:=wdGoToBookmark, Name:=BookMarkName
End If

DsastrayAuthor Commented:
As you can see in the code, and in the file I've linked to above, the individual page I am deleting (and conversely would be adding) is a section unto itself. This should hopefully make my stated goal easier.

My question is primarily in reference to the chart sections of the document. I would ultimately like to build a single template page, and then add duplicate pages after it, while changing the source of the linked chart to reflect the position of the new page.

ie I build the page which hosts the chart for "STOR ISJ & LASCIIONTE BOEVIHAE, 2006 Draft Report Compliance Program, Level 1" [linked to excel chart: AzRfs1]

I would like to automate the production of 3 (near) duplicates after this template page which would host:
"STOR ISJ...Program, Level 2" [linked to excel chart: AzRfs2]
"STOR ISJ...Program, Level 3" [linked to excel chart: AzRfs3]
"STOR ISJ...Program, Level 4" [linked to excel chart: AzRfs4]
all with the same page title, colored box behind the chart, page orientation, footnotes etc (ascending page numbers, of course)

This way, I need only produce 1 of each chart while I designing these damned reports.

I believe I know how to change the link source programatically, otherwise you'll be hearing from me in a different question.

DsastrayAuthor Commented:
Weelio: the table solution is not really the solution I am looking for, but having said that...

I'm curious to see you take a crack at a nicely formatted version of say, page 2 using your word tables method. Note the links in the text, as well as the linked table in the corner.

Also, the document has to be usable to be fiddled with by upper management, who don't take particularly kindly to messing up the way they've been doing things for the past 5 years.

I am afraid that might well be for entertainment puposes only, as they say, as redesigning the document doesn't hold much appeal.

DsastrayAuthor Commented:
You are!

DsastrayAuthor Commented:
Am I the only one who goes nuts when all the Microsoft examples do not declare their variables?

I was always under the impression that "Option Explicit" was the only way to go, and then I can never use their sample code "out of the box."

Not a huge deal when i and DocNum are obviously integers, but it's esp. frustrating when I'm learning a new concept and I don't rightly have any idea what type of object the variable I am trying to learn is! Aargh.

I'll just save this rant here and post it on the appropriate newsgroup someday.
DsastrayAuthor Commented:
Weelio, in the spirit of Good Faith, (ie I have already awarded points) will you help me troubleshoot this? I am having a few issues.

If I can't resolve immediately I will post further.
DsastrayAuthor Commented:
Initial impression: if linked objects, ie the tables and charts are set to "Move object with text" it does not work well, they end up in awfully weird places.

(This is possibly b/c their position, when "relative" as opposed to "absolute" may be dependent on text from previous pages.)
William ElliottSr Tech GuruCommented:
first off whomever made these documents, did so without learning anything about desktop publishing.. then only know how to make it look like they know what they are doing by moving little boxes here and there.

i stick by what i said about using tables. they force structure, no matter how the pages are viewed.
open the document and click on View - web layout or view - normal layout.

you will see everything is out of place
William ElliottSr Tech GuruCommented:
to answer your question, yes everything seems to be ina 'relative' place.

oh,.. and i don't declare my variables half the time..  if i am going to use a script for a prolonged period of time, then i go back and make it option explicit and declare everything and comment each line to explain the process..  but i make several scripts a day here and at work,. so i don't put all the detail into them,.. especially since i am not only working a full schedule and going to school full time,.. i'm also here hepling people out,..  heh
William ElliottSr Tech GuruCommented:
why not make pdf's or images out of the document, that way it will never change
DsastrayAuthor Commented:
Man, if I had it my way, this would all be done using InDesign. Word is such a faux desktop publisher, I think the more you know about desktop publishing the harder it is to use.

IIRC InDesign has unfortunately awkward/weak dynamic linking abilities, I believe that was why I ultimately decided not to go with it.

Interesting, re: variables. Do you find a performance hit from undeclared vars significant or just an old wives' tale?

I have a few routines that I run where a few extra milliseconds/kbs wouldn't matter, but a few that run a 1 minute routine 1000 times. Like you say, those are the ones I am careful to comment out etc.

My biggest irritation doesn't come from not doing it in everyday practice or for small scripts, but rather the laziness of bypassing it in a help file, one where the declaration helps understand the underlying process.
William ElliottSr Tech GuruCommented:
i started out in the technical field actally doing desktop publishing with adobe pagemaker. within the app everything had a specific placeholder and no matter how it was formatted it looked good. so i tell beginners to use word for basic web design now,. otherwise i only user work for outlines and basic documentation.

i can understand why word was used as far as OLE, but whoever designed these documents, should not be designing documents.(my personal opinion) hire a kid out of school to go through and rework them properlly.

most of my scripts are basic automation or configuration routines.  pulling and pasting AD data or server data.  nothing too terribly substantial, so i don't see a difference when declaring or not declaring.  my only issue is usually forgetting to prograamatically close an excel window, then after  a few dozens tests i look in task manager to find it full of excel.exe

gettign back to the document in question. do you see what i mean, when you change the views?

DsastrayAuthor Commented:
Yep, absolutely.

So you don't like the document from a design point of view, or is there something else you are referring to?
William ElliottSr Tech GuruCommented:
click on
View - web layout or
view - normal layout
and you see how it is all 'wonky'
that is what i'm referring to as changing the views.

and from the design point of view i don't like the document, because since there is not 'static' connectors anywhere or fixed locations, then you can change one thing and all the dynamic placeholders can be messed up.
DsastrayAuthor Commented:
Yeah, it's funny you should mention that, I am going through right now and changing the properties of all the text boxes etc. to make them static. I believe this is accomplished (or at least my approach is)

by turning off "Move Object with Text"
by making certain everything is positioned by Absolute Position x inches right of Page (not column etc)
turning off "Lock Anchor"

Is that correct?
William ElliottSr Tech GuruCommented:
i am not even going to begin to pretend that i can answer that question regarding this document. yes that seams like a decent approach, but i'm sure re configuring the document from teh ground up would be a more optimal approach.
All Courses

From novice to tech pro — start learning today.