none
Field find RRS feed

  • Question

  • I want to find {NUMPAGES} field in all pages and need to update the value. How can I use Find() method of Word to do this? Pls help.


    Please "Mark as Answer" if this post answered your question. :)
    Thursday, June 16, 2011 11:11 AM

Answers

  • Hi Cham,

    You could try:

    Sub UpdateFields()
    Application.ScreenUpdating = False
    With ActiveDocument
      .Fields.Update
      .PrintPreview
      .ClosePrintPreview
    End With
    Application.ScreenUpdating = True
    End Sub

    or

    Sub UpdateFields()
    Dim Rng as Range
    For each Rng in ActiveDocument.StoryRanges
      Do
        Rng.Fields.Update
        set Rng = Rng.NextStoryRange
      Loop until Rng is nothing
    Next
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, June 17, 2011 5:20 AM

All replies

  • Hi Cham

    You can update all fields in the body of a Word document using doc.Content.Fields.Update. No need to use Find.

    If NumPages is elsewhere than the body of the document then you need to use the Range of that particular Story (look up StoryRange in the object model help).


    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 12:38 PM
    Moderator
  • Thanks Cindy,

    I have more than 1 {NumPages} variables in whole document and I need to update them all. If you can please simplify the answer how to use StoryRange, it will be a great help.

    Thanx..


    Please "Mark as Answer" if this post answered your question. :)
    Friday, June 17, 2011 3:48 AM
  • Hi Cham,

    You could try:

    Sub UpdateFields()
    Application.ScreenUpdating = False
    With ActiveDocument
      .Fields.Update
      .PrintPreview
      .ClosePrintPreview
    End With
    Application.ScreenUpdating = True
    End Sub

    or

    Sub UpdateFields()
    Dim Rng as Range
    For each Rng in ActiveDocument.StoryRanges
      Do
        Rng.Fields.Update
        set Rng = Rng.NextStoryRange
      Loop until Rng is nothing
    Next
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, June 17, 2011 5:20 AM