PDA

View Full Version : Swapping Pages with a macro HELP



Diffy
12-28-2006, 10:49 AM
I have a document that lists information about various campaigns. Each page has the same first line:

Campaign Name:<tab> [Campaigns name here]

There are about 60 or so pages that have this.

I need the pages to be in order alphabetically by the Campaign name.

Is there a way I can put them in order? I was thinking maybe one of you macro wiz's could come up with something, or at least give me an idea of how to do this.

Thanks,

Dif

XLGibbs
12-30-2006, 10:24 AM
I am sure there is a way to do this...let me play around with it and see if I can come up with anything, word is not my expertise though! But since you haven't gotten a response yet...I will give it a shot.

fumei
12-31-2006, 06:39 PM
How are the pages made as pages? Is there a hard page break? A section break?

fumei
12-31-2006, 06:45 PM
Further questions:

Are there multiple campaign names with the same starting letter? if so, how do you want them sorted?

BTW: is this a school project?

Diffy
01-07-2007, 06:48 PM
How are the pages made as pages? Is there a hard page break? A section break?


Every single page break was made by using Insert -> Break -> Page break

I think that is what you want to know.


Further questions:

Are there multiple campaign names with the same starting letter? if so, how do you want them sorted?

Yes there are, and I would like them sorted alphebetically... first letter then second letter etc.

BTW: is this a school project?

No. It is actually something I was trying to do for work. Right now we have this enormous document but it is a real pain to look up info on a specific sales campaign because there is no order to it.

It is not something I HAVE to do but something that would help the sales team out.

fumei
01-08-2007, 05:41 AM
I hate to tell you this, but...yes, this is possible, but it is not trivial. The problem lies with "page". Pages are not independent things in Word. So you can not sort "pages".

Having the sub-sort with names starting with the same letter vastly increases the difficulty.

Just be clear. You are talking about individual pages...there is NO, ZERO, ZIP, NADA content that goes over to another page. It is ONLY single pages to deal with.

Please confirm. If there IS possible content that covers more than a single page - again, it is possible, but it increases the difficulty even more.

fumei
01-08-2007, 10:34 AM
There may be other possible avenues, but here is one way. As stated, the issue is with how Word deals with "pages".

Say, you have the document like this:

Campaign name:<tab>Bordo Bongo
Campaign name:<tab>Bardo
Campaign name:<tab>Arfff
Campaign name:<tab>Cordo
Campaign name:<tab>V8 juice
Campaign name:<tab>Munch much Bordo
Campaign name:<tab>Many times Bordo

You did not state if a campaign name can have spaces (multiple words), but I am assuming it may be possible. This is an issue with this method, but it can be fixed.

The RESULT you want is:
Campaign name:<tab>Arfff
Campaign name:<tab>Bardo
Campaign name:<tab>Bordo Bongo
Campaign name:<tab>Cordo
Campaign name:<tab>Many times Bordo
Campaign name:<tab>Munch much Bordo
Campaign name:<tab>V8 juice

That is, sorted alphabetically, including names with the same starting letter - the "Many", Munch".

Because it would be very difficult to track the pages moving them around IN the document, the easiest way is to create a NEW document, inserting the pages in alphabetical order.

So, first of all...the ASSUMPTIONS:

A) dealing with single pages, separated by page breaks
B) each page is consistently started by a paragraph "Campaign Name:<tab>campaign name. That is, after the actual campaign name is a paragraph mark.

Here it is, as pseudo-code. I have actually written this, and it works.


Step ONE is to bookmark each page.

1. go to the start of the doc
2. make a range of the page by using CSet to move the range end until it finds Chr(12), a page break
3. make a string of Paragraph(1) - "Campaign Name...."
4. from that, extract all text after "Campaign Name:<tab>".
5. clear ALL spaces out of that extraction, as we are going to use it for a bookmark name (no spaces allowed). So "Many times Bordo" has to become "ManytimesBordo".
6. make a bookmark of the current range (current page) and name it the extracted string (eg. "ManytimesBordo", "Bardo")
7. collapse the range to its End
8. move the range forward TWO characters
9. repeat for the number of pages in the document. This is done with a For Next loop like:

For var = 0 to NumberOfPages - 1

OK, now you have the original document with each page content individually bookmarked, and named for the actual campaign.

Word sorts, by default, bookmarks alphabetically. This is handy as it means...your sort is already done.

Assuming the original document has NO other bookmarks(!), Step TWO is to copy all the pages BY BOOKMARK.

1. make a document object of the original doc
2. make a new document object
3. start another For Next loop

For var = 0 to bookmark count

4. make a range object of the original document bookmark (index = var + 1)
5. activate the new document
6. add a new bookmark using the range (bookmark) Name
7. make this new bookmark range = the range of the original document bookmark range. This makes a copy of the original document bookmark range in the new document.
8. move the selection to the end of the document
9. activate back to the original document

This will loop through all the bookmarks in the original document, copying them over to the new document. As the order of bookmarks is alphabetically, it will in fact recreate the new document with the pages sorted alphabetically, based on the name picked up as the campaign name.

Diffy
02-12-2007, 04:39 PM
I just completed it, from your directions. I can not thank you enough for your help. :rotlaugh:

By the way, your art is truely wonderful. I spent a good 30 minutes or so looking at all of it and I must say it is very impressive.

fumei
02-13-2007, 09:10 AM
Excellent! I am always happy when someone takes pseudo-code and makes it real code. That means you probably learned something. That is better than just handing over a working solution.

Thank you re the art.

mdmackillop
02-18-2007, 07:34 AM
Belatedly came across this thread.
Glad you got it working,
An alternative method, and something that is useful in any case for large documents is to make use of Headers and Outline view.
BTW, do this with a copy as it will change some formats until you get Styles set appropriately.
1. Change to outline view and promote your Name lines to Header 1 (This could be done by VBA)
2. Collapse so that only the Headers are visible. You can then highlight and drag into order.
3. Navigating the document can now be accomplished within the Outline view.
I've attached a very simple sample.
Regards
MD

fumei
02-18-2007, 04:19 PM
2. Collapse so that only the Headers are visible. You can then highlight and drag into order. What???? You meaning manually???????

mdmackillop
02-18-2007, 04:41 PM
What???? You meaning manually???????
It's good exercise and I bet I could do it quicker than creating the VBA from scratch!
Anyway, not everyone will have 60 pages to sort and a manual method is always handy to keep in reserve.

fumei
02-18-2007, 05:04 PM
Very true. And is a good example of - it is not always what we CAN do, but we need to do.

As for faster, probably. Although I used an existing procedure I have to make every page bookmarked. It sits in my global store of procedures precisely because there is sometimes a need for quickly getting contents by page.

The whole exercise for my testing took about six minutes.