PDA

View Full Version : Copy selected pages to new Doc



assafh21
02-02-2012, 11:58 AM
Hello,
I'm new with VBA in Word.
I use MS Office 2007 at home and 2010 at work
I would like to know how can I select range of pages, copy the selection into new document
for that I created Userform with 2 TextBox and 1 CommandButton
but I don't know what should be the code

Many thanks
Assaf

fumei
02-03-2012, 08:26 PM
If you are new to VBA why are you doing anything with textboxes and a commabutton What are they supposed to be doing? The first thing to doing code is to define and figure out EXACTLY - and I mean EXACTLY - what you want to do.

assafh21
02-03-2012, 09:05 PM
Hi,
The TextBoxs are for the From/To pages and the Commandbutton to execute the code of copy and paste those pages into new document.
I know how to thing like this in Excel but no idea in Word... :)
Assaf

fumei
02-03-2012, 10:39 PM
"The TextBoxs are for the From/To pages"

What does that mean? From where ? To where? And WHAT is going from someplace to whatever is the other place? Please describe it EXACTLY.

assafh21
02-04-2012, 01:53 AM
I would like to know how can I select range of pages, copy the selection into new document
For that I created Userform with 2 TextBox (for From page and To page) and CommandButton to run the macro of copy the selected pages, create new document and then paste the selection.
I hope my description is clear now....:)

If anyone knows, please help.
Assaf

Frosty
02-04-2012, 01:06 PM
Unlike Excel, which has a clearly defined "page" (a worksheet), Word really has nothing like that. Despite what looks like different "pages", that is simply a description of what the document will look like on the printer you are currently attached to.

In short-- pages are very very fluid, depending on a lot of things. The major "blocks" of formatting in Word are the following:
Characters. ("a" and "b" and "c"), which are always contained with a...
Paragraph. ("Fuzzy Wuzzy was a Bear. Fuzzy Wuzzy had no hair.[paragraph mark]") which are contained within a...
Section.

You can use paragraph formatting to say "page break before" to define the "beginning" of a page...and you can use a "special" character called a "Page break" character to cause a page break to happen immediately after it... and you can use a section break to also start a new section, but dynamically selecting "Page 3" is not actually as possible as you might think.

Even though the Print dialog allows you to print pages 2-5, that actual page range is defined at the moment of printing, not before.

With those difficulties, that is why you have to really define what you want to do.

It may be simpler to start developing your macro as such:
1. Force the user to manually select what you want to create the new document from.
2. Copy the selection.
3. Create a new document (if the new document might be based on different templates, this is a chance to ask the user which template in a user form)
4. Paste into the new document.

But you have to ask yourself- what steps are you really saving? CTRL + C gives a copy. CTRL + N gives a new blank document. CTRL + V pastes into that new document. So that is three short cut keys. The trick is in selecting the range, which is why you want to create the macro.

So, since we know Word doesn't really give you access to specific pages (since that is so fluid), you need to more clearly spell out why users would select a certain group of "pages" over another group of pages... What is it about those pages that would cause the user to select those pages?

In that question, is the answer to your original question.

But the *short* answer to your question is: you can't.

fumei
02-04-2012, 02:22 PM
And if you are selecting a range of pages you are NOT on a userform (generally speaking). So again, please explain exactly what you want to do. Selecting a range of pages is simply too vague and does not mean anything.

If what you mean is: I want to put 5 in the From box, 7 in the To box, and have pages 5 to 7 copied to a new document...then SAY so.

If that is what you wanty to do, then I strongly suggest just simply copying it. As you do not know what you are doing with code, working through this is not a very good place to start working with code.

That being said, again, please state exactly what you want to happen.

The difficulty with this general request is the issue Ftosty mentions: "page" is a very loose thing in Word.

assafh21
02-05-2012, 12:41 PM
Hi there

First many thanks
After brainstorming with my colleagues we decided that the "To" is not needed because we actually need the "From" page till the end of the document.
For that I used the next code:

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=Val(Me.TextBox1.Value)
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Copy
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
Selection.Paste

instead of "Normal" we used "Template" but here is the problem...
In the footer I have PAGE of NUMPAGES
for example if document has 10 pages and I copy from page 8,
I get page 8 of 2, how can change it to be page 8 of 10 (PAGE + NUMPAGES)

Many thanks
Assaf