How to select whole document including header and foter

J

Jan Kronsell

I have the following code

Selection.WholeStory
Selection.Fields.Update

that i use to update fields, but it does not updatefields in the header and
footer.

I have tried adding

Selection.HeaderFooter

But gives an error (Ivalid use of property).

How can I update all the fields, not matter where they are?

Jan
 
J

Jan Kronsell

I got it myself:

This will do the job:

Sub FileSave()
ActiveDocument.Fields.Update
n = ActiveDocument.Sections.Count
For i = 1 To n
ActiveDocument.Sections(i).Headers(1).Range.Fields.Update
ActiveDocument.Sections(i).Footers(1).Range.Fields.Update
Next i
ActiveDocument.save
End Sub

But I still wonder if it can done in an easier way.

Jan
 
J

Jean-Guy Marcil

Jan Kronsell was telling us:
Jan Kronsell nous racontait que :
I got it myself:

This will do the job:

Sub FileSave()
ActiveDocument.Fields.Update
n = ActiveDocument.Sections.Count
For i = 1 To n
ActiveDocument.Sections(i).Headers(1).Range.Fields.Update
ActiveDocument.Sections(i).Footers(1).Range.Fields.Update
Next i
ActiveDocument.save
End Sub

But I still wonder if it can done in an easier way.

You could try:

Application.PrintPreview = True
Application.PrintPreview = False

The screen will flash, but it is fairly quick, this will also update the
fields in the document body. It will not work if Automatically update fields
is turned off in the print options, but this is on by default.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jan Kronsell

Thanks, Ill try that.


Jan

Jean-Guy Marcil said:
Jan Kronsell was telling us:
Jan Kronsell nous racontait que :


You could try:

Application.PrintPreview = True
Application.PrintPreview = False

The screen will flash, but it is fairly quick, this will also update the
fields in the document body. It will not work if Automatically update fields
is turned off in the print options, but this is on by default.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Charles Kenyon

Much better is the macro at http://www.gmayor.com/installing_macro.htm. This
will pick up all headers/footers.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Chuck

This works for me. Note that I've included the MakeHFValid public sub which
ensures that all headers/footers are activated storyranges for editing (per
the MVPS website):

Sub UpdateAllFields()

Dim rngRange As Range

With ActiveDocument

.Fields.Update

For Each rngRange In ActiveDocument.StoryRanges
Do
rngRange.Fields.Update
Set rngRange = rngRange.NextStoryRange
Loop Until rngRange Is Nothing
Next

End With

End Sub

Public Sub MakeHFValid()
'
'Required for global find and replace to make sure HeadersFooters StoryRange
is activated
'
Dim lngJunk As Long
Dim hdrHeader As HeaderFooter
Dim i As Integer

For i = 1 To ActiveDocument.Sections.Count
For Each hdrHeader In ActiveDocument.Sections(i).Headers
lngJunk = hdrHeader.Range.StoryType
Next hdrHeader
Next i

End Sub
 
J

Jan Kronsell

Thank you all for your help.

I have an additional question though. When a user closes a document that has
not previously been saved, he is asked if he wants to save it or not, How do
I test what key the user uses?

I guess it must be sometinh like

If something = vbYes

but what is something? And what should I call my sub to catch it? I tried
with FileClose and FileQuit but to no use.

Jan
 

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