Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Wed, 5 Mar 2008 14:04:13 -0800 (PST),    group: microsoft.public.word.vba.beginners        back       


Looping Through Pages of a Word Doc With VBA   
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
date: Wed, 5 Mar 2008 14:04:13 -0800 (PST)   author:   unknown

Re: Looping Through Pages of a Word Doc With VBA   
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?

-- 
Enjoy,
Tony

 wrote in message 
news:6417fb24-64a3-4f19-87f4-9f6a1ad67283@s12g2000prg.googlegroups.com...
> 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
date: Thu, 6 Mar 2008 09:24:18 -0000   author:   Tony Jollans My forename at my surname dot com

Re: Looping Through Pages of a Word Doc With VBA   
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.
date: Thu, 6 Mar 2008 08:58:02 -0800 (PST)   author:   unknown

Re: Looping Through Pages of a Word Doc With VBA   
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.

-- 
Enjoy,
Tony

 wrote in message 
news:7ccee365-757e-493d-ae53-f40381544d04@h11g2000prf.googlegroups.com...
> 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.
date: Thu, 6 Mar 2008 17:37:59 -0000   author:   Tony Jollans My forename at my surname dot com

Re: Looping Through Pages of a Word Doc With VBA   
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.

Tony Jollans wrote:
>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.
>
>> 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.

-- 
Message posted via http://www.officekb.com
date: Thu, 06 Mar 2008 18:31:29 GMT   author:   fumei via OfficeKB.com u37563@uwe

Re: Looping Through Pages of a Word Doc With VBA   
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?
date: Mon, 10 Mar 2008 15:16:11 -0700 (PDT)   author:   unknown

Re: Looping Through Pages of a Word Doc With VBA   
Check out the .Information property of the Selection object.  It can give 
the the number of the page on which the selection is located.

-- 
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

 wrote in message 
news:d530875b-d2ed-444e-8776-5f7458927a88@e6g2000prf.googlegroups.com...
> 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?
date: Tue, 11 Mar 2008 21:36:34 +1000   author:   Doug Robbins - Word MVP

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us