Updating fields 
Author Message
 Updating fields

I'd like to update all fields in a document, irrespective of whether they
are in the header, footer, or main text. I currently use:

    dim objField as Field

    For each objField in ActiveDocument.fields
        objField.Update
    next

However, this seems to miss out the fields in the header and footer. Is
there a nice way of forcing Word to update all fields, without looping
through the headers and footers as well?

Furthermore, my documents often have different sections with different
headers. If I was to loop through the header and footer of a document, would
this be compromised by having different sections with different headers /
footers?

Thanks,
Mark.



Fri, 31 Dec 2004 17:06:33 GMT  
 Updating fields
Hi Mark,

    ActiveDocument.PrintPreview
    ActiveDocument.ClosePrintPreview

Please post any response to the newsgroups for the benefit of others who may
also be following the thread.

Hope this helps,
Doug Robbins - Word MVP

Quote:
> I'd like to update all fields in a document, irrespective of whether they
> are in the header, footer, or main text. I currently use:

>     dim objField as Field

>     For each objField in ActiveDocument.fields
>         objField.Update
>     next

> However, this seems to miss out the fields in the header and footer. Is
> there a nice way of forcing Word to update all fields, without looping
> through the headers and footers as well?

> Furthermore, my documents often have different sections with different
> headers. If I was to loop through the header and footer of a document,
would
> this be compromised by having different sections with different headers /
> footers?

> Thanks,
> Mark.



Fri, 31 Dec 2004 17:28:45 GMT  
 Updating fields
Doug,

Thanks for such a quick response.

I was aware that manually doing a print preview would force the fields to be
updated, although I was looking for a neater solution in code. Do you know
why updating the fields in the document misses out the header and footer,
and why a print preview doesn't?

TIA,
Mark.



Quote:
> Hi Mark,

>     ActiveDocument.PrintPreview
>     ActiveDocument.ClosePrintPreview

> Please post any response to the newsgroups for the benefit of others who
may
> also be following the thread.

> Hope this helps,
> Doug Robbins - Word MVP


> > I'd like to update all fields in a document, irrespective of whether
they
> > are in the header, footer, or main text. I currently use:

> >     dim objField as Field

> >     For each objField in ActiveDocument.fields
> >         objField.Update
> >     next

> > However, this seems to miss out the fields in the header and footer. Is
> > there a nice way of forcing Word to update all fields, without looping
> > through the headers and footers as well?

> > Furthermore, my documents often have different sections with different
> > headers. If I was to loop through the header and footer of a document,
> would
> > this be compromised by having different sections with different headers
/
> > footers?

> > Thanks,
> > Mark.



Fri, 31 Dec 2004 17:26:31 GMT  
 Updating fields
Hi Mark,

Quote:
> although I was looking for a neater solution in code

try this one:

  Dim oRange As Range

  For Each oRange In ActiveDocument.StoryRanges
    oRange.Fields.Update
  Next oRange

Quote:
> why updating the fields in the document misses out the header and
> footer?

its by design, the same problem occurs with search/replace.

Greetings from Germany

Lutz, MVP Word/VBA



Fri, 31 Dec 2004 18:19:09 GMT  
 Updating fields
Lutz,

I've tried your code, but fields in the document header are still not
updated correctly. Any other ideas?

Thanks,
Mark.


Quote:
> Hi Mark,

> > although I was looking for a neater solution in code

> try this one:

>   Dim oRange As Range

>   For Each oRange In ActiveDocument.StoryRanges
>     oRange.Fields.Update
>   Next oRange

> > why updating the fields in the document misses out the header and
> > footer?

> its by design, the same problem occurs with search/replace.

> Greetings from Germany

> Lutz, MVP Word/VBA



Fri, 31 Dec 2004 18:31:09 GMT  
 Updating fields
Hi Mark,

A Word document consists of up to 11 different story ranges, and for macro
operations, it is necessary to cycle through all of those story ranges to
have the operation performed on the whole document.  See the article "Using
a macro to replace text where ever it appears in a document including
Headers, Footers, Textboxes, etc." at:

http://www.mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

for an explanation.

I guess print preview catches everything as it goes through the process of
preparing the document for printing.

Please post any response to the newsgroups for the benefit of others who may
also be following the thread.

Hope this helps,
Doug Robbins - Word MVP

Quote:
> Doug,

> Thanks for such a quick response.

> I was aware that manually doing a print preview would force the fields to
be
> updated, although I was looking for a neater solution in code. Do you know
> why updating the fields in the document misses out the header and footer,
> and why a print preview doesn't?

> TIA,
> Mark.



> > Hi Mark,

> >     ActiveDocument.PrintPreview
> >     ActiveDocument.ClosePrintPreview

> > Please post any response to the newsgroups for the benefit of others who
> may
> > also be following the thread.

> > Hope this helps,
> > Doug Robbins - Word MVP


> > > I'd like to update all fields in a document, irrespective of whether
> they
> > > are in the header, footer, or main text. I currently use:

> > >     dim objField as Field

> > >     For each objField in ActiveDocument.fields
> > >         objField.Update
> > >     next

> > > However, this seems to miss out the fields in the header and footer.
Is
> > > there a nice way of forcing Word to update all fields, without looping
> > > through the headers and footers as well?

> > > Furthermore, my documents often have different sections with different
> > > headers. If I was to loop through the header and footer of a document,
> > would
> > > this be compromised by having different sections with different
headers
> /
> > > footers?

> > > Thanks,
> > > Mark.



Fri, 31 Dec 2004 18:31:55 GMT  
 Updating fields
Doug,

Thanks for the link. It's very annoying that something as simple as updating
all the fields in the document requires so much code, and that even when you
go to such lengths, there are some cases in which the code won't work.

Thanks for all your help everyone. I'll consider using the Print Preview
hack since it should work for everything, but I don't like it ;-)

Mark.



Quote:
> Hi Mark,

> A Word document consists of up to 11 different story ranges, and for macro
> operations, it is necessary to cycle through all of those story ranges to
> have the operation performed on the whole document.  See the article
"Using
> a macro to replace text where ever it appears in a document including
> Headers, Footers, Textboxes, etc." at:

> http://www.mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

> for an explanation.

> I guess print preview catches everything as it goes through the process of
> preparing the document for printing.

> Please post any response to the newsgroups for the benefit of others who
may
> also be following the thread.

> Hope this helps,
> Doug Robbins - Word MVP


> > Doug,

> > Thanks for such a quick response.

> > I was aware that manually doing a print preview would force the fields
to
> be
> > updated, although I was looking for a neater solution in code. Do you
know
> > why updating the fields in the document misses out the header and
footer,
> > and why a print preview doesn't?

> > TIA,
> > Mark.



> > > Hi Mark,

> > >     ActiveDocument.PrintPreview
> > >     ActiveDocument.ClosePrintPreview

> > > Please post any response to the newsgroups for the benefit of others
who
> > may
> > > also be following the thread.

> > > Hope this helps,
> > > Doug Robbins - Word MVP


> > > > I'd like to update all fields in a document, irrespective of whether
> > they
> > > > are in the header, footer, or main text. I currently use:

> > > >     dim objField as Field

> > > >     For each objField in ActiveDocument.fields
> > > >         objField.Update
> > > >     next

> > > > However, this seems to miss out the fields in the header and footer.
> Is
> > > > there a nice way of forcing Word to update all fields, without
looping
> > > > through the headers and footers as well?

> > > > Furthermore, my documents often have different sections with
different
> > > > headers. If I was to loop through the header and footer of a
document,
> > > would
> > > > this be compromised by having different sections with different
> headers
> > /
> > > > footers?

> > > > Thanks,
> > > > Mark.



Fri, 31 Dec 2004 18:54:23 GMT  
 Updating fields
Doug,

Following your advice, I now use a small routine to cycle through all the
story ranges of a document which is copied below.

However, this fails to work correctly on a document with different headers
for odd and even pages. On a document with the first 3 pages separated by
section breaks (odd pages), I find that the first Even Page Header fails to
update. Any ideas?

I also find it doesn't update the TOC, hence why I added a loop to iterate
through the TOC's in a document. I thought the TOC was a field and would
update just like any others?

TIA,
Mark.

PS Sorry for CC'ing this to you directly, but I wanted to develop the thread
which is now quite old and easily missed.

Public Sub UpdateAllFields()
    Dim objRange As Range
    Dim objTOC As TableOfContents

    'Turns off Screen Updating so that user does not see what is happening.
    Word.Application.ScreenUpdating = False

    For Each objRange In ActiveDocument.StoryRanges
        objRange.Fields.Update

        Do While Not (objRange.NextStoryRange Is Nothing)
            Set objRange = objRange.NextStoryRange
            objRange.Fields.Update
        Loop
    Next

    'Update TOC individually as word does not include it as a field.
    'Allows for more than one TOC.
    For Each objTOC In ActiveDocument.TablesOfContents
        objTOC.Update
    Next

    'Turns back on Screen Updating.
    Word.Application.ScreenUpdating = True
End Sub



Quote:
> Hi Mark,

> A Word document consists of up to 11 different story ranges, and for macro
> operations, it is necessary to cycle through all of those story ranges to
> have the operation performed on the whole document.  See the article
"Using
> a macro to replace text where ever it appears in a document including
> Headers, Footers, Textboxes, etc." at:

> http://www.mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

> for an explanation.

> I guess print preview catches everything as it goes through the process of
> preparing the document for printing.

> Please post any response to the newsgroups for the benefit of others who
may
> also be following the thread.

> Hope this helps,
> Doug Robbins - Word MVP


> > Doug,

> > Thanks for such a quick response.

> > I was aware that manually doing a print preview would force the fields
to
> be
> > updated, although I was looking for a neater solution in code. Do you
know
> > why updating the fields in the document misses out the header and
footer,
> > and why a print preview doesn't?

> > TIA,
> > Mark.



> > > Hi Mark,

> > >     ActiveDocument.PrintPreview
> > >     ActiveDocument.ClosePrintPreview

> > > Please post any response to the newsgroups for the benefit of others
who
> > may
> > > also be following the thread.

> > > Hope this helps,
> > > Doug Robbins - Word MVP


> > > > I'd like to update all fields in a document, irrespective of whether
> > they
> > > > are in the header, footer, or main text. I currently use:

> > > >     dim objField as Field

> > > >     For each objField in ActiveDocument.fields
> > > >         objField.Update
> > > >     next

> > > > However, this seems to miss out the fields in the header and footer.
> Is
> > > > there a nice way of forcing Word to update all fields, without
looping
> > > > through the headers and footers as well?

> > > > Furthermore, my documents often have different sections with
different
> > > > headers. If I was to loop through the header and footer of a
document,
> > > would
> > > > this be compromised by having different sections with different
> headers
> > /
> > > > footers?

> > > > Thanks,
> > > > Mark.



Mon, 03 Jan 2005 23:13:00 GMT  
 Updating fields
Hi Mark,

If you go back to my initial advice it was to use a macro to cause the
document to be previewed and then the preview screen closed.

   ActiveDocument.PrintPreview
   ActiveDocument.ClosePrintPreview

Please post any response to the newsgroups for the benefit of others who may
also be following the thread.

Hope this helps,
Doug Robbins - Word MVP

Quote:
> Doug,

> Following your advice, I now use a small routine to cycle through all the
> story ranges of a document which is copied below.

> However, this fails to work correctly on a document with different headers
> for odd and even pages. On a document with the first 3 pages separated by
> section breaks (odd pages), I find that the first Even Page Header fails
to
> update. Any ideas?

> I also find it doesn't update the TOC, hence why I added a loop to iterate
> through the TOC's in a document. I thought the TOC was a field and would
> update just like any others?

> TIA,
> Mark.

> PS Sorry for CC'ing this to you directly, but I wanted to develop the
thread
> which is now quite old and easily missed.

> Public Sub UpdateAllFields()
>     Dim objRange As Range
>     Dim objTOC As TableOfContents

>     'Turns off Screen Updating so that user does not see what is
happening.
>     Word.Application.ScreenUpdating = False

>     For Each objRange In ActiveDocument.StoryRanges
>         objRange.Fields.Update

>         Do While Not (objRange.NextStoryRange Is Nothing)
>             Set objRange = objRange.NextStoryRange
>             objRange.Fields.Update
>         Loop
>     Next

>     'Update TOC individually as word does not include it as a field.
>     'Allows for more than one TOC.
>     For Each objTOC In ActiveDocument.TablesOfContents
>         objTOC.Update
>     Next

>     'Turns back on Screen Updating.
>     Word.Application.ScreenUpdating = True
> End Sub



> > Hi Mark,

> > A Word document consists of up to 11 different story ranges, and for
macro
> > operations, it is necessary to cycle through all of those story ranges
to
> > have the operation performed on the whole document.  See the article
> "Using
> > a macro to replace text where ever it appears in a document including
> > Headers, Footers, Textboxes, etc." at:

> > http://www.mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

> > for an explanation.

> > I guess print preview catches everything as it goes through the process
of
> > preparing the document for printing.

> > Please post any response to the newsgroups for the benefit of others who
> may
> > also be following the thread.

> > Hope this helps,
> > Doug Robbins - Word MVP


> > > Doug,

> > > Thanks for such a quick response.

> > > I was aware that manually doing a print preview would force the fields
> to
> > be
> > > updated, although I was looking for a neater solution in code. Do you
> know
> > > why updating the fields in the document misses out the header and
> footer,
> > > and why a print preview doesn't?

> > > TIA,
> > > Mark.



> > > > Hi Mark,

> > > >     ActiveDocument.PrintPreview
> > > >     ActiveDocument.ClosePrintPreview

> > > > Please post any response to the newsgroups for the benefit of others
> who
> > > may
> > > > also be following the thread.

> > > > Hope this helps,
> > > > Doug Robbins - Word MVP


> > > > > I'd like to update all fields in a document, irrespective of
whether
> > > they
> > > > > are in the header, footer, or main text. I currently use:

> > > > >     dim objField as Field

> > > > >     For each objField in ActiveDocument.fields
> > > > >         objField.Update
> > > > >     next

> > > > > However, this seems to miss out the fields in the header and
footer.
> > Is
> > > > > there a nice way of forcing Word to update all fields, without
> looping
> > > > > through the headers and footers as well?

> > > > > Furthermore, my documents often have different sections with
> different
> > > > > headers. If I was to loop through the header and footer of a
> document,
> > > > would
> > > > > this be compromised by having different sections with different
> > headers
> > > /
> > > > > footers?

> > > > > Thanks,
> > > > > Mark.



Tue, 04 Jan 2005 15:56:35 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Using--After Update-- to update field in table.

2. Updating fields: footer doesn't update right

3. Newbie Update field in DB using AutoNumber field

4. updating fields in table from field in another table

5. Newbie Update field in DB using AutoNumber field

6. auto update fields

7. Updating field in form from summary value in another

8. VB - Update Field from Other Table

9. Update Field Value

10. Using VBA to scan and update fields

11. Updating Fields with Different Values?

12. Updating Field From Another Table Using Random

 

 
Powered by phpBB® Forum Software