MS Office Forum / Word / Programming / March 2008
Looping Through Pages of a Word Doc With VBA
|
|
Thread rating:  |
osirun@gmail.com - 05 Mar 2008 22:04 GMT Hi there,
I'm very new to VBA so I don't really know if I what I want to do is even possible, much less how to go about it.
What I want to do is, given a document, to loop through it, find all the frames, and change their height. I can do this part. Here's the hard part though: I want to change the frame height to a different value if it's on a page with a heading. I also want the frame height value to be different depending on how many lines there are in the heading (if the heading is several lines then the frame should be shorter).
Does any of this sound possible to do with VBA?
Many thanks,
Matthew Marcus
Tony Jollans - 06 Mar 2008 09:24 GMT Ignore VBA for a minute, there are basic Word issues here.
First of all, although you say headING, I suspect you mean headER. Before going much further can you confirm which you mean?
HeadINGs are paragraphs preceding blocks of text. They may appear anywhere on a page and resizing Frames around them will most likely cause them to move. There may be multiple headings, quite possibly at different levels and of different sizes, on a page.
HeadERs are blocks of text and pictures that appear generally at the tops of pages, although technically they could appear anywhere on the page. The basic content and placement of elements is defined at Section level and, by and large, all pages in a Section, have the same Header although, again, technically every page could be different if the basic content contains conditional logic or reference to document content. Checking the content at page level is not possible in VBA - well, it does depend on version, but even where possible it is very difficult.
There may be many effects of changing Frame heights, in extreme cases changing earlier pages in the document. What are you trying to achieve?
 Signature Enjoy, Tony
> Hi there, > [quoted text clipped - 14 lines] > > Matthew Marcus osirun@gmail.com - 06 Mar 2008 16:58 GMT Thanks for jumping in, Tony!
I did mean (I think) heading rather than header. Basically the page may have a title, like "Lesson 1 - Ecosystems" at the top of it. If it does have such a title, then the frame should begin at a lower level than the title. So if the title is "Lesson 1 - Ecosystems blah blah blah several lines of extra text" the sidebar frame should begin even lower down.
To get what I'd ideally want to happen to happen, I'd have to (a) be able to locate given page objects on given pages of the document, (b) detect for a heading object how many lines of space it takes takes up, then (c) find any frame that exists on the same page and change its height property accordingly.
As I say, I have no idea if VBA for Word can achieve something this complicated, but I thought there was no harm in asking! Cheers.
Tony Jollans - 06 Mar 2008 17:37 GMT Well, this can probably be done but it isn't simple. This is page layout stuff, for which Publisher is probably a better application. I'm not sure I can visualise the circumstances where you need to be doing this, but ...
Off the top of my head I don't think I'd try to analyse the page the way you are suggesting. Frames are anchored to paragraphs. If you anchor your frame to the paragraph immediately following the heading and position it relative to the paragraph then it should start in the right place; determining the correct height is a bit more involved but I still think I would use the paragraph following the heading as a guide and subtract its position (paragraph_ref.Range.Information(wdVerticalPositionRelativeToPage)) from the page depth or something along those lines.
 Signature Enjoy, Tony
> Thanks for jumping in, Tony! > [quoted text clipped - 13 lines] > As I say, I have no idea if VBA for Word can achieve something this > complicated, but I thought there was no harm in asking! Cheers. fumei - 06 Mar 2008 18:31 GMT I am with Tony on this. Possible, but non-trivial. And for the same reason. This is high-level layout, and is more appropriate for a layout type application. While Word attempts (badly some times) to be a layout application, the reality is that it is NOT a layout application.
Are your headings using an explicit Style? If not, then that makes it even more non-trivial.
>Well, this can probably be done but it isn't simple. This is page layout >stuff, for which Publisher is probably a better application. I'm not sure I [quoted text clipped - 14 lines] >> As I say, I have no idea if VBA for Word can achieve something this >> complicated, but I thought there was no harm in asking! Cheers. osirun@gmail.com - 10 Mar 2008 23:16 GMT Okay, well, I can loop through all the headings using something like this (cannibalised) code:
Sub CopyAllHeadings() Dim NewDoc As Document Dim SrcRg As Range, DestRg As Range
Set SrcRg = ActiveDocument.Range
Set NewDoc = Documents.Add
With SrcRg.Find .ClearFormatting .Text = "" .Format = True .Style = ActiveDocument.Styles("Heading 2") .Forward = True .Wrap = wdFindStop
Do While .Execute 'when a piece of Body Text is found, 'SrcRg covers its range Set DestRg = NewDoc.Range DestRg.Collapse wdCollapseEnd DestRg.FormattedText = SrcRg.FormattedText & " " & ActiveDocument.Bookmarks("\Page").Range.FormattedText Loop End With
NewDoc.Save 'will display Save dialog
Set SrcRg = Nothing Set DestRg = Nothing Set NewDoc = Nothing End Sub
I can also loop through all the frames in the document and set their style.
The trick would be to be able to loop through all the headings - as above - but be able to find out, for each one, what page it is on, and correlate this somehow with a loop to find any frame on the same page, then access that frame's attributes and set them accordingly. Is this mad?
Doug Robbins - Word MVP - 11 Mar 2008 12:36 GMT Check out the .Information property of the Selection object. It can give the the number of the page on which the selection is located.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Okay, well, I can loop through all the headings using something like > this (cannibalised) code: [quoted text clipped - 40 lines] > then access that frame's attributes and set them accordingly. Is this > mad?
|
|
|