Link to home
Start Free TrialLog in
Avatar of Dsastray
DsastrayFlag for United States of America

asked on

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.
Avatar of William Elliott
William Elliott
Flag of United States of America image

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
Avatar of Dsastray

ASKER

So, do you feel like a layout as seen in the file

http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=4082

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
i don't suppose it will allow you to export to html, then duplicate the html for the page?
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
an example
http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=4084 

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.
Apologies for taking so long. I felt it was necessary to generate a better generic file before I posted the word document:

https://filedb.experts-exchange.com/incoming/ee-stuff/4172-KAKA.zip

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
      wordApp.ActiveDocument.Bookmarks("\Section").Range.Delete
End If

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.


Thanks
D
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.

ASKER CERTIFIED SOLUTION
Avatar of William Elliott
William Elliott
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are!

Thx!
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.
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.
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.)
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
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
why not make pdf's or images out of the document, that way it will never change
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.
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?

Yep, absolutely.

So you don't like the document from a design point of view, or is there something else you are referring to?
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.
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?
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.