Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Jul 2011
    Christchurch, New Zealand
    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:

    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
            .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.Collapse wdCollapseEnd
        End With
    End Sub
    But doesn't seem to be working at all. Any help would be much appreciated.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Melbourne, Victoria, Australia
    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.
    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

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

    myronr (2012-03-15)

  5. #3
    New Lounger
    Join Date
    Jul 2011
    Christchurch, New Zealand
    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.

Posting Permissions

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