help with paragraph identification

A

ajmilton

The project i'm working on right now involves reading information at
the beginning of a paragraph (for example: [Level 5]). I need to be
able to get this information from any point in the paragraph.
Currently, I'm using comments to do this - I can retrieve the
information using ActiveDocument.Comments(incrementedVariable).Range.Text.
The problem I'm running into is, a page break in the middle of a
paragraph throws this all out of whack. Working with
paragraphs().Range, word seems to consider the beginning of a new page
a new paragraph as well.
I'm not at all opposed to completely redoing what i've done thus far,
I just need help figuring out how to carry the identifier across a
page break.
Oh, system information - using Word 2002 on win2k.
Thanks in advance for any help.
-aj milton
 
C

Cindy M -WordMVP-

Hi Ajmilton,
The project i'm working on right now involves reading information at
the beginning of a paragraph (for example: [Level 5]). I need to be
able to get this information from any point in the paragraph.
Currently, I'm using comments to do this - I can retrieve the
information using ActiveDocument.Comments(incrementedVariable).Range.Text.
The problem I'm running into is, a page break in the middle of a
paragraph throws this all out of whack. Working with
paragraphs().Range, word seems to consider the beginning of a new page
a new paragraph as well.
I'm not at all opposed to completely redoing what i've done thus far,
I just need help figuring out how to carry the identifier across a
page break.
This should only be happening if you've inserted a manual page break? Then
it would, indeed, be a new paragraph. Why are manual page breaks being
inserted?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Cindy M -WordMVP- said:
Hi Ajmilton,

This should only be happening if you've inserted a manual page break? Then
it would, indeed, be a new paragraph. Why are manual page breaks being
inserted?

cindy-
I don't recall inserting a manual page break, but I might have. I'll
check this on monday at work and see what happens.
thanks
-aj
 
A

ajmilton

cindy-
I don't recall inserting a manual page break, but I might have. I'll
check this on monday at work and see what happens.
thanks

Ok, so i was wrong and had put in a manual page break. That problem's
fixed.
As one is fixed, another appears ... I'm having fun with headers now.

I need to set the headers per page, determined by the paragraph of the
highest level on the page. I can find the highest level no problem,
and i can set the headers, but the code i'm using is setting the
header across the entire document.

I've tried a couple methods: working with sections, and working with
seekview
a)
Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text =
maxLev

b)
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:=maxLev
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

in both cases, the headers are altered everywhere in the document.
I've tried putting in ActiveDocument.Bookmarks("\Page").Range.Select
before adding the header, with no luck.

Any ideas on this?

-aj
 
A

Alex Ivanov

Headers are not the best place for variable information. Try to use
textboxes in the main story for that; they can overlap headers or footers if
you want.
 
C

Cindy M -WordMVP-

Hi Ajmilton,
I need to set the headers per page, determined by the paragraph of the
highest level on the page.
I'm not sure what you mean by "highest level", but if you're talking
outline levels, and the document is using styles to set these (which
would be correct design), then what you need is the STYLEREF field in
the header.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Cindy M -WordMVP- said:
Hi Ajmilton,

I'm not sure what you mean by "highest level", but if you're talking
outline levels, and the document is using styles to set these (which
would be correct design), then what you need is the STYLEREF field in
the header.

I apologize for not being more clear in this - "highest level" is
determined by text at the beginning of each paragraph - "[Level 5]"
for example.
The first part of the macro i've written runs through the entire
document and puts the "level" value in a comment to make the level a
bit more accessible.
The second part of the macro moves through the document, presumably
page by page, and is intended to set the header (and footer, but once
header is working, footer shouldn't be a challenge) to represent the
highest level.

I've gotten the second part to properly go through the document page
by page. It also finds the "highest level" correctly. The problem now
is, depending on which method I use to set the headers, either the
header gets overwritten (result is the header reflects the highest
level on the last page), or the levels for each page are added to the
header (result is something like "Level 3Level 4Level 2").

What I *want* is for each page's header to be unique to that page. The
data isn't hard to get, the problem is keeping each page's header from
changing.

Hope this clarifies the problem a bit.
-aj
 
C

Chad DeMeyer

Ajmilton,

Maybe I'm oversimplifying your problem and you already knew this, but keep
in mind that in order for each page to have a unique header, each page needs
to be in a separate section (i.e., separated from the previous page by a
section break) and the header for each section has to have the 'Same as
Previous' setting turned off.

Regards,
Chad DeMeyer


ajmilton said:
Cindy M -WordMVP- <[email protected]> wrote in message
Hi Ajmilton,

I'm not sure what you mean by "highest level", but if you're talking
outline levels, and the document is using styles to set these (which
would be correct design), then what you need is the STYLEREF field in
the header.

I apologize for not being more clear in this - "highest level" is
determined by text at the beginning of each paragraph - "[Level 5]"
for example.
The first part of the macro i've written runs through the entire
document and puts the "level" value in a comment to make the level a
bit more accessible.
The second part of the macro moves through the document, presumably
page by page, and is intended to set the header (and footer, but once
header is working, footer shouldn't be a challenge) to represent the
highest level.

I've gotten the second part to properly go through the document page
by page. It also finds the "highest level" correctly. The problem now
is, depending on which method I use to set the headers, either the
header gets overwritten (result is the header reflects the highest
level on the last page), or the levels for each page are added to the
header (result is something like "Level 3Level 4Level 2").

What I *want* is for each page's header to be unique to that page. The
data isn't hard to get, the problem is keeping each page's header from
changing.

Hope this clarifies the problem a bit.
-aj
 
C

Cindy M -WordMVP-

Hi Ajmilton,

Both answers from Alex and Chad give you the basic information you
need.

Just to summarize it all in one place:

1. Word's headers and footers are section properties; you can't set
them on a "per page" basis. (If that's how you had to work they'd be
pretty useless as headers/footers :))

2. This means you need to put something into the main document body,
just positioned over the header area where you want to see it. Alex
suggests a textbox. Personally, i prefer working with Frames (not the
web kind, the Word kind) because I can define them as part of a STYLE.

Whether you use a text box or a frame, you need to keep in mind that
Word provides no way to "tack" a graphical object to a specific page.
Such objects are always anchored to a paragraph in the text and will
appear on the same page as that paragraph. If your macro is simply the
last step before publication, this shouldn't present a problem. But if
people will be editing the document, you could be in trouble.
I apologize for not being more clear in this - "highest level" is
determined by text at the beginning of each paragraph - "[Level 5]"
for example.
The first part of the macro i've written runs through the entire
document and puts the "level" value in a comment to make the level a
bit more accessible.
The second part of the macro moves through the document, presumably
page by page, and is intended to set the header (and footer, but once
header is working, footer shouldn't be a challenge) to represent the
highest level.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Chad DeMeyer said:
Ajmilton,

Maybe I'm oversimplifying your problem and you already knew this, but keep
in mind that in order for each page to have a unique header, each page needs
to be in a separate section (i.e., separated from the previous page by a
section break) and the header for each section has to have the 'Same as
Previous' setting turned off.

I think using separate sections is going to be the best bet for this
project. Having said that, I've been able to make the headers on the
first page different from the rest of the headers in the document, but
I'm now having the same problem with the remaining headers. from page
2 (also section 2) to the end of the document, the headers all change
to whatever the last section sets it.

The code I'm using to set headers follows:

Code:
For intPage = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
ActiveDocument.Bookmarks("\Page").Range.Select
'a small if/else if goes here, just determines what the maximum
'level' on the page is - Level 1/Level 2 etc.. and stores it in maxLev
to be used below
Set myrange = Selection.Bookmarks("\Page")
With ActiveDocument.Sections(intPage)
If .Headers(wdHeaderFooterPrimary).LinkToPrevious Then
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
End If
.Headers(wdHeaderFooterPrimary).Range.Text = maxLev
.Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment
= wdAlignParagraphCenter
End With

myrange.Range.InsertBreak Type:=wdSectionBreakContinuous
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute,
Count:=intPage + 1
Next


On a side note, at one point, I was using
Selection.PageSetup.DifferentFirstPageHeaderFooter = True to see if
this setting was messing something up ... when I set it to true, all
of the headers disappeared...

again, thanks for the input.
-aj
 
C

Cindy M -WordMVP-

Hi Ajmilton,
I think using separate sections is going to be the best bet for this
project.
Before you head down this path, it might be a good idea to find out if
inserting that many sections could turn into a problem for you. One
can tell you don't know much about Word, how it should be used, and
where problems could crop up :)

How is the end-product of your code going to be used?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Cindy M -WordMVP- said:
Hi Ajmilton,

Before you head down this path, it might be a good idea to find out if
inserting that many sections could turn into a problem for you. One
can tell you don't know much about Word, how it should be used, and
where problems could crop up :)

How is the end-product of your code going to be used?

Cindy -
You're right, this is the second project i've worked on with word, and
to be honest, coding macros for word is driving me up the wall :p
What sort of problems could arise by inserting several sections?
The goal of this code is to parse though a document, determine the
highest level on the page (as referenced earlier - the level per
paragraph will be marked at the beginning of each paragraph), and
reflect the highest level in the header/footer of each page.
A note to consider - the level i'm referring to has nothing to do with
word objects, it's determined by text at the beginning of each
paragraph.
again, thanks for the help
-aj
 
C

Chad DeMeyer

When you insert a section break, the break is inserted immediately preceding the Range or Selection object. So if I'm not confused, your code is putting a continuous section break at the start of each page. The problem with this is that the header you will see on that page will be the header for the first section on the page, which is not the same section that the maxlevel paragraph for that page is in. That may be part of your problem. I suggest trying to add a Next Page section break at the end of each page, other than the last, rather than using Continuous section breaks, which are really just useful for laying out columns or designing online forms. Also, I recommend inserting the section break first, and then performing the operations on the new section's header and footer second.

Regards,
Chad DeMeyer


ajmilton said:
Chad DeMeyer said:
Ajmilton,

Maybe I'm oversimplifying your problem and you already knew this, but keep
in mind that in order for each page to have a unique header, each page needs
to be in a separate section (i.e., separated from the previous page by a
section break) and the header for each section has to have the 'Same as
Previous' setting turned off.

I think using separate sections is going to be the best bet for this
project. Having said that, I've been able to make the headers on the
first page different from the rest of the headers in the document, but
I'm now having the same problem with the remaining headers. from page
2 (also section 2) to the end of the document, the headers all change
to whatever the last section sets it.

The code I'm using to set headers follows:

Code:
For intPage = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
ActiveDocument.Bookmarks("\Page").Range.Select
'a small if/else if goes here, just determines what the maximum
'level' on the page is - Level 1/Level 2 etc.. and stores it in maxLev
to be used below
Set myrange = Selection.Bookmarks("\Page")
With ActiveDocument.Sections(intPage)
If .Headers(wdHeaderFooterPrimary).LinkToPrevious Then
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
End If
.Headers(wdHeaderFooterPrimary).Range.Text = maxLev
..Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment
= wdAlignParagraphCenter
End With

myrange.Range.InsertBreak Type:=wdSectionBreakContinuous
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute,
Count:=intPage + 1
Next


On a side note, at one point, I was using
Selection.PageSetup.DifferentFirstPageHeaderFooter = True to see if
this setting was messing something up ... when I set it to true, all
of the headers disappeared...

again, thanks for the input.
-aj
 
C

Cindy M -WordMVP-

Hi Ajmilton,
What sort of problems could arise by inserting several sections?
The goal of this code is to parse though a document, determine the
highest level on the page (as referenced earlier - the level per
paragraph will be marked at the beginning of each paragraph), and
reflect the highest level in the header/footer of each page.
Which still doesn't answer the key question: what's the purpose of
the end result (the document) you're processing?

The problem with sections is that they're not terribly stable. Or,
perhaps the better formulation is: if a document is going to "go
bad" anywhere, most frequently it will be "in the section breaks".
There are quite a number of section-specific properties (headers,
footers, columns...), and this information is "stored in the section
break".

So, if you have lots of section breaks, there's a higher chance the
document will "break". If you're just processing a document that
will be printed, then thrown away, you should be OK. OTOH, if
someone is going to be editing the document after your code has run
on it, you not only have to consider the "fragility" problem, but
also the fact that, as the user edits, things may move around, and
suddenly your section breaks and page information will no longer be
in the right place.

If you need to generate a more "robust" document, then I'd still
look at using the StyleRef field, and possibly applying a character
style specific for this purpose to the text you want to show in the
header/footer.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Chad DeMeyer said:
When you insert a section break, the break is inserted immediately
preceding the Range or Selection object. So if I'm not confused, your
code is putting a continuous section break at the start of each page.
The problem with this is that the header you will see on that page will
be the header for the first section on the page, which is not the same
section that the maxlevel paragraph for that page is in. That may be
part of your problem. I suggest trying to add a Next Page section break
at the end of each page, other than the last, rather than using
Continuous section breaks, which are really just useful for laying out
columns or designing online forms. Also, I recommend inserting the
section break first, and then performing the operations on the new
section's header and footer second.

Chad-
I played some with the wdSectionBreakNextPage - it seems to not only
insert a section break but a new page with it. I'll fiddle some with
this and see if this will work any better
thanks
-aj
 
A

ajmilton

Cindy M -WordMVP- said:
I'm not sure what you mean by "highest level", but if you're talking
outline levels, and the document is using styles to set these (which
would be correct design), then what you need is the STYLEREF field in
the header.

Hello again, haven't pestered you folks for a while now. I've got the
styleref thing working ... sort of.
The problem I'm running up against now is, the text I want to appear
in the headers ("Level 1", "Level 2", etc.) doesn't appear in the
document ... just referenced to ("(L1)", "(L2)", and so on).
I have a couple ideas that might work, but I havent been able to make
any of them function, and I'm wondering if anybody has some input on
making one work, or a possible alternate method.
1) apply the style to a comment:
The first thing I've tried is to apply the style used by the
STYLEREF (I call it "HighLevel") to a comment. I've succeeded in
attaching "HighLevel" to the comments. The problem I've run into is
.... STYLEREF doesn't seem to include comments in its search for the
style. Quite problematic.

2) apply the style to hidden text:
This is a somewhat less desirable method- I'm not sure what sort
of effect inserting random hidden text on each page will have on
layouts and margins and whatnot. Also, in my tests of this, I can't
figure out how to set the hidden property programmatically on the
style. I haven't put as much effort into this option because I was
hoping to work all the problems out with option 1.

Looking forward to some responses :)
-aj milton
 
C

Cindy M -WordMVP-

Hi Ajmilton,
I've got the
styleref thing working ... sort of.
The problem I'm running up against now is, the text I want to appear
in the headers ("Level 1", "Level 2", etc.) doesn't appear in the
document ... just referenced to ("(L1)", "(L2)", and so on).
I have a couple ideas that might work, but I havent been able to make
any of them function, and I'm wondering if anybody has some input on
making one work, or a possible alternate method.
2) apply the style to hidden text:
I think this is the way you're going to have to go. Put a \! switch at
the end of the StyleRef field to override the "hidden" formatting in
the field result.

To include hidden in the style def:
.Font.Hidden = True

hidden text shouldn't affect page layout, as long as it's not being
displayed or printed. You control this programmatically through
ActiveWindow.View.ShowHiddenText = False
Application.Options.PrintHiddenText = False

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

A vaguely related question to all this - is there some way to insert
text and apply a style to it at the same time? The insert commands
don't seem to return anything like an index for me to use for
selection.
thanks again
-aj milton, the man of many questions
 
C

Cindy M -WordMVP-

Hi Ajmilton,
A vaguely related question to all this - is there some way to insert
text and apply a style to it at the same time? The insert commands
don't seem to return anything like an index for me to use for
selection.
Well, best with Word is (almost) always to work with the RANGE object.
Selection should only be used
- if the focus should be positioned for the user before handing
over control
- you're working with something in the object model that relies on
selection (such as the \page bookmark)

This is how I'd go about inserting text and formatting it. I'm
assuming you're using some variation of .Find to pick up your "level"
information:

Dim rngSearch as Word.Range
Dim rngNewText as Word.Range

Set rng = doc.range

Do
With rng.Find
'Criteria stuff, here
If .Found = True then
'We want to keep the original range
'for continuing the search, so
'use an additional range for
'adding the data. You have to use
'.Duplicate (sorta like "Clone")
'otherwise you'd manipulate the same range!
Set rngNewText = rngSearch.Duplicate
'move ahead of the found term
rng.Collapse wdCollapseStart
rng.Text = "new text"
rng.Style = "style name"
End If
End with
Loop While .Found = True

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
A

ajmilton

Well, if it isn't one thing, it's always something else.
This insertion/style stuff with range worked quite well, but your
recommendation earlier to use the \! switch to remove the hidden
format from the STYLEREF field isn't working. I searched some and
found the \*charformat switch, tried it, searched some more and found
out that \*charformat seems to not work in styleref.
Any ideas why the \! switch doesn't work either?

On another note, I'm thinking about repaginating the document before
applying styles so I don't have to worry about paragraphs split across
pages. I haven't started looking at this idea yet, but if there's a
straightfoward way to check for a paragraph breaking across a page,
I'd appreciate some direction toward that as well.

many thanks, as always
-aj milton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top