Calling macro function from within a header/footer?

J

Jezebel

The question makes no sense. Parts of a document don't 'use' a macro; people
do. What are you trying to achieve?
 
M

ML

I want to have a the ability for code within the header or footer to make
use of a function. Basically I have a bunch of code within the header that
handles checking for security tag fields (sequence markers) on each page
(nested if else statements) to determine the header security setting for
that page. I'd like to have this done in a function if possible instead of
nested statements inside the header itself.
 
J

Jezebel

Word documents just don't work this way. Headers and footers are displayed
text. You can't run code there. Nor are they specific to the page: headers
and footers belong to the section. Nor do they have security settings.
 
M

ML

I am not referring to Word security, I am referring to text tags to indicate
the page level security to the reader.

Conditional headers/footers are possible at the page level as I am using
them now with nested IF statements that check the page for sequence numbers.
What I'd like to be able to do is do this in VBA code as I need to do more
complex checks.
 
J

Jay Freedman

We've asked Microsoft about the ability to run VBA code from a field
within a document. The answer is that it probably "could" be a feature
but it won't ever be allowed -- because the ability to run arbitrary
code from a field would be a security risk (in the antivirus security
sense).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
M

ML

Ok. So is there anyway possible to access a field via VBA code in the
header and footers? I need to be able to set the field in a header/footer
on a page by page basis in the same way that conditional headers/footers
handle it, so that each page is unique, not just the section itself.
 
T

Tony Jollans

I thought Jay was pretty clear; this can't be done now and won't ever be
allowed.

You cannot get a macro to run automatically other than on designated events.
If you're happy for the updates to happen before a Save, or Print, say, then
a macro can do whatever you want it to do. Even via code, though, the only
way to get different headers on a page, rather than section, basis requires
conditional page-number-based logic in fields in the header.
 
J

Jay Freedman

No. When you put a field in a header, the header shows the same field
result on every page of the section, regardless of whether you
manipulate the field's code manually or with VBA.

If you can't do what you need with IF fields or StyleRef fields, the
only solution is to put in section breaks so that each page is a
separate section, with Same As Previous turned off, and have a macro
insert the appropriate text (not a field) directly into each section's
header.

You aren't the first -- or the last -- to ask for something like this,
for security levels or revision dates or similar page-by-page info.
There has never been a satisfactory answer within Word.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

You aren't the first -- or the last -- to ask for something like this,
for security levels or revision dates or similar page-by-page info.
There has never been a satisfactory answer within Word.

Jay,

I don't agree with you here. The method that Peter Jamieson helped me work
out for security classification levels seems to work just fine for page by
page header/footer annotations provided the appropriate security marker is
located on each page.

See Advanced Conditional Headers and Footers here:
http://gregmaxey.mvps.org/Conditional_Headers_Footers.htm
 
J

Jay Freedman

You aren't the first -- or the last -- to ask for something like this,
for security levels or revision dates or similar page-by-page info.
There has never been a satisfactory answer within Word.

Jay,

I don't agree with you here. The method that Peter Jamieson helped me work
out for security classification levels seems to work just fine for page by
page header/footer annotations provided the appropriate security marker is
located on each page.

See Advanced Conditional Headers and Footers here:
http://gregmaxey.mvps.org/Conditional_Headers_Footers.htm

My apologies, Greg -- I hadn't read your article before. That looks
like a perfect solution for ML's problem. I assume the existing
security tags can be changed to the fields you describe by an
appropriate Replace operation.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
M

ML

Is it possible to still do the page based logic in a macro? Right now I do
it via the conditional headers with page number based logic, but I want to
move this from fields in the header with If statements to a macro if
possible.
 
M

ML

That is the method I use now, but our security levels and markers are much
more complex so I need to be able to do the code checks in a macro as the IF
statements in the header are now way too complex and large. Can this be
done in a macro, to loop through each page and set the header field as it
would have been done with the referenced conditional header approach?
 
G

Greg Maxey

Jay,

I thought that you would agree. If my opinion, Peter's solution was more
than satisfactory. It was brilliant. After 27 years (at the time) of
laboriously crafting classification headings, Peter's nested fields solution
was like manna from heaven.

I have never tried to replace existing (C) or (S) markers with field code.
As most document revisions require a classification review I have simple
replaced the markers with the field code using an AutoText as I do the
review. However, since find and replace with Autotext is a simple matter, I
am sure that it can be done.
 
J

Jay Freedman

At the risk of repeating myself -- NO! The mechanism is what it is.

Conditional fields (IF or StyleRef) have a single field code
throughout a multipage section, although the visible result depends on
the conditions on the page. This cannot be manipulated by a macro --
if you change something in the field, it will be repeated throughout
the section, not just on one page.

The alternative is to split the document into one-page sections, and
use a macro to change each section's header -- the important point
being that the header belongs to the section, and will differ from
page to page *only* because each section contains only one page.

There are no other options.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
M

ML

Ok thanks Jay and no need to shout or repeat yourself.

The fact that you didn't even think the conditional page by page headers was
possible by any means in prior posts makes me take what you say with a
grain/bag of salt, and granted your not the first MVP not to know something
or be wrong and wont be the last. I appreciate your help but perhaps try
not to be so black and white with your advice, when it is obvious you don't
know it all 100%.
 
J

Jonathan West

Hi ML,

I've just reviewed the entire thread, and I think it would be worth
providing you with on overview of what can and cannot be done with regard to
page headers and footers.

Broadly speaking, there are two types of information that you can put into a
page header. These are

1. Text
2. Fields

Text will display the same on every page to which the header applies.

Fields might or might not display the same - the field is separately
recalculated for each page on which it occurs in the header.

There is no means in Word by which a Field can make use of the result of a
VBA function. Word does not provide this feature. i.e. there is no
CUSTOMFUNCTION field in Word in the way that there is a DOCPROPERTY or
DOCVARIABLE field allowing you to insert the contents of a custom property
or document variable. Therefore, you cannot have different pages displaying
different text depending on the result of a VBA function.

Even if you have a VBA function which you trigger by some other means, you
would still have to find some method of inserting the result of the function
into the header. You can't insert it as text, because that would appear the
same on every page on which the header displays.

That leaves you with 3 options

1. Use a nested field sufficiently complex that different text appears on
every page. This is doable but very difficult for a document more than a few
pages long.

2. Insert section breaks within your document so that different pages have
different headers, and insert your information as text in each separate
header

3. Don't place the information in the header at all. Instead, put it as text
on the page itself. You can have the text appear to be in the header area by
putting the text in a textbox which is positioned up in the header even
through the anchor of the textbox is somewhere in the body of the page.

All three options have their different drawbacks. Whichever you choose, I'm
sure we will be happy to help as far as possible with overcoming the
limitations.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

ML

Jonathan, thanks for the info.

2 and 3 are not options as it makes for too complex a document for the user,
as they need to be able to just add new pages as normal pages and not have
to deal with adding a new section or textbox to each page they add. Option
1 we are using now but it needs to be made more complex for our needs and
hence the issue. Is there anyway possible to access the fields of the
headers in Option 1 but from a VBA macro that would basically run through
each page, check page codes, and then set the page level header field?
 
T

Tony Jollans

There have been a few other posts since this but I'll add my reply here
anyway - it doesn't contradict what's been said in other posts.

If you are content to update your headers, let's say, just before printing
rather than in real time then I think what you want can be done - it isn't
easy, but it can be done.

What you need to do is to work out in VBA what needs to be on the header in
each page and write out a header for the section (or the odd pages of the
section, or whatever headers you've got) that looks, perhaps, like this:

{ If { Page } = 1 "Text for page 1 header" }{ If { Page } = 2 "Text for page
2 header" }

Writing nested fields from VBA is fiddly but can be done in a couple of
different ways - here's one which uses the selection (usually best avoided)

ActiveDocument.Sections(1).Headers(1).Range.Select
Selection.Collapse Direction:=wdCollapseEnd

With Selection
.Fields.Add Range:=.Range, Type:=wdFieldIf, _
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=.Range, Type:=wdFieldPage, _
PreserveFormatting:=False
.TypeText " = 1 "
.TypeText Chr(34)
.TypeText "String for page 1"
.TypeText Chr(34)
.MoveRight Unit:=wdCharacter, Count:=1
End With
 
J

Jonathan West

ML said:
Jonathan, thanks for the info.

2 and 3 are not options as it makes for too complex a document for the
user, as they need to be able to just add new pages as normal pages and
not have to deal with adding a new section or textbox to each page they
add. Option 1 we are using now but it needs to be made more complex for
our needs and hence the issue. Is there anyway possible to access the
fields of the headers in Option 1 but from a VBA macro that would
basically run through each page, check page codes, and then set the page
level header field?

No there is no way of doing that. The field (or nested set of fields) in the
header has to be capable of displaying to appropriate text on each page in
order for different text to be displayed, without different fields being set
on different pages. This is because without inserting section breaks, you
don't actually have different fields on the different pages - you have a
single field (or nested set) in a single header which is calculated
differently according to the page number.

I realise this is not the answer you want to hear, but you have been asking
the same question in a number of different ways for some time. If the answer
you wanted was going to appear, it would have done so by now.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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