Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2012, 07:39 PM
b0x4it b0x4it is offline Change all fields to just numbers in current page! Windows 7 32bit Change all fields to just numbers in current page! Office 2010 32bit
Advanced Beginner
Change all fields to just numbers in current page!
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default Change all fields to just numbers in current page!

I have been using this code for a log time to convert all the fields of the document to just numbers:

Code:
Sub MMD_MakeAllReferencesToJustNumber()
    On Error Resume Next
    Dim oStoryRng As Range
    Dim oFld As Field
    For Each oStoryRng In ActiveDocument.StoryRanges
        For Each oFld In oStoryRng.Fields
            If oFld.Type = wdFieldRef Then
                If InStr(oFld.Code.Text, "\# 0;0") = 0 Then
                 oFld.Code.Text = Replace(oFld.Code.Text, " \h", " \# 0;0 \h")
                End If
             oFld.Update
            End If
        Next oFld
    Next oStoryRng
End Sub
but if the document is a long one with lots of field, then it takes long time to process all fields every time I run this code, because it updates all fields in the current document.

Now, I am wondering if it is possible to just update all the fields in the current page by changing



Code:
For Each oStoryRng In ActiveDocument.StoryRanges
to something just for the current page. I appreciate your help in this matter.
Thanks,
Reply With Quote
  #2  
Old 04-24-2012, 09:02 PM
macropod's Avatar
macropod macropod is offline Change all fields to just numbers in current page! Windows 7 64bit Change all fields to just numbers in current page! Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi b0x4it,

Word doesn't really have a page-level definition, though it is possible to obtain a page range for processing. A simpler approach is to just select the range you're interested in (could be as little as a single word, or might span two or more pages) and run a macro like the following:
Code:
Sub MMD_MakeSelectedReferencesToJustNumber()
On Error Resume Next
Dim oFld As Field
  For Each oFld In Selection.Fields
    With oFld
      If .Type = wdFieldRef Then
        With .Code
          If InStr(.Text, "\# 0;0") = 0 Then .Text = Replace(.Text, " \h", " \# 0;0 \h")
        End With
      .Update
    End If
  End With
Next oFld
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-24-2012, 09:23 PM
b0x4it b0x4it is offline Change all fields to just numbers in current page! Windows 7 32bit Change all fields to just numbers in current page! Office 2010 32bit
Advanced Beginner
Change all fields to just numbers in current page!
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default

thanks for your prompt reply
How about selecting the current paragraph and then running your code using the following code?

Code:
Sub MMD_MakeReferencesInParagToJustNumber()

    On Error Resume Next
    Dim oFld As Field
    Selection.GoTo what:=wdGoToBookmark, Name:="\sel"
    For Each oFld In Selection.Fields
        If oFld.Type = wdFieldRef Then
            'add charformat switch:
            If InStr(oFld.Code.Text, "\# 0;0") = 0 Then
                oFld.Code.Text = Replace(oFld.Code.Text, " \h", " \# 0;0 \h")
            End If
            'updates the field results to display the new format
            oFld.Update
        End If
    Next oFld

End Sub
Reply With Quote
  #4  
Old 04-24-2012, 10:08 PM
macropod's Avatar
macropod macropod is offline Change all fields to just numbers in current page! Windows 7 64bit Change all fields to just numbers in current page! Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi b0x4it,

The code I posted will work on a single word, a sentence, paragraph, or the whole document if that's selected. I'm not sure what you're hoping to achieve via your enhancement and I have no idea what you mean by:
Quote:
running your code using the following code
since my code isn't what followed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-24-2012, 10:44 PM
b0x4it b0x4it is offline Change all fields to just numbers in current page! Windows 7 32bit Change all fields to just numbers in current page! Office 2010 32bit
Advanced Beginner
Change all fields to just numbers in current page!
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default

I have added this to your code:

Code:
Selection.GoTo what:=wdGoToBookmark, Name:="\sel"
I am trying to automatically select the current paragraph and then run the code modifying all the field in the selection that is current paragraph. But, it does not work! Any suggestion?
Reply With Quote
  #6  
Old 04-24-2012, 10:49 PM
macropod's Avatar
macropod macropod is offline Change all fields to just numbers in current page! Windows 7 64bit Change all fields to just numbers in current page! Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi b0x4it,

'\sel' has nothing to do with a paragraph - it's just the current selection.
Quote:
I am trying to automatically select the current paragraph and then run the code modifying all the field in the selection that is current paragraph.
In that case, simply change:
For Each oFld In Selection.Fields
to:
For Each oFld In Selection.Paragraphs.First.Range.Fields
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-24-2012, 11:06 PM
b0x4it b0x4it is offline Change all fields to just numbers in current page! Windows 7 32bit Change all fields to just numbers in current page! Office 2010 32bit
Advanced Beginner
Change all fields to just numbers in current page!
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default

thanks a lot!
it's working!
cheers,
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save the current page in a new file with all the page settings (header, footer Jamal NUMAN Word 6 03-15-2012 03:27 PM
Change all fields to just numbers in current page! Page Numbers Not Matching Chapter Numbers gracie5290 Word 1 02-02-2012 11:41 PM
Change all fields to just numbers in current page! How to call current PC date and/or current PC year KIM SOLIS Excel 2 11-04-2011 06:09 PM
Change all fields to just numbers in current page! Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM
Change all fields to just numbers in current page! Page numbers...starting on page 3 Motiv8td1 Word 2 02-21-2009 07:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft