Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Jul 2011
    Location
    Christchurch, New Zealand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Updating only page numbers in Word doc

    I'm trying to update just the page numbers in my word document using this macro:

    Code:
    Sub UpdatePageNumber()
    '
    ' UpdatePageNumber Macro
    ' Store cursor position and go to top of document
        Dim rngTemp As Word.Range
        Set rngTemp = Selection.Range
        Selection.HomeKey wdStory
        With Selection.Find
            .ClearFormatting
            .Text = "^d PAGEREF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            While .Execute = True
                Selection.Fields.Update
                Selection.Collapse wdCollapseEnd
            Wend
        End With
    End Sub
    But doesn't seem to be working at all. Any help would be much appreciated.
    Cheers,
    M

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    There are two possible explanations for your problem.

    Firstly that type of search may not work unless you first toggle the field codes so you can see the field codes. You could try adding a couple of lines to your code to assist the search.
    ActiveWindow.View.ShowFieldCodes = True
    'your code here
    ActiveWindow.View.ShowFieldCodes = False

    Secondly, page numbers are typically found in headers and/or footers. The search you have coded does not search these stories. Instead, I would rework the code to address this need another way.
    Code:
    Sub UpdatePageRefFields()  Dim aStory As Range, aFld As Field
      For Each aStory In ActiveDocument.StoryRanges
        For Each aFld In aStory.Fields
          If aFld.Type = wdFieldPageRef Then aFld.Update
        Next aFld
      Next aStory
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    myronr (2012-03-15)

  4. #3
    New Lounger
    Join Date
    Jul 2011
    Location
    Christchurch, New Zealand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow! I spend a whole day trying to figure this out and you manage it with a couple lines of code . Thanks Andrew, you're a life saver.
    -Myron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •