vbscript to search Word document for 8 digit numbers and display page number

Author Message
john7a8

  • Total Posts : 581
  • Scores: 35
  • Reward points : 0
  • Joined: 1/25/2010
  • Location: Orlando, FL
  • Status: offline
vbscript to search Word document for 8 digit numbers and display page number Friday, December 16, 2011 9:23 AM (permalink)
0
I have a vbscript that opens a Word document and uses regexp to search for 8 digit numbers.  That part is working.  I now would like to display the page number of where the 8 digit number was found.  I am trying to use 'wdActiveEndPageNumber', but it is always printing the last page of the document as the page number.  Below is the script.  Can anyone tell me what I am going wrong?
TIA!
John
----------------------------------
Const wdStatisticPages  = 2
Const wdActiveEndPageNumber = 3

Dim sFSpec : sFSpec     = "Word.docx"    

Dim oFS    : Set oFS    = CreateObject( "Scripting.FileSystemObject" )
Dim wrdApp : Set wrdApp = CreateObject("Word.Application")
Dim wrdDoc : Set wrdDoc = wrdApp.Documents.Open( oFS.GetAbsolutePathName( sFSpec ) )

WScript.Echo "Pages:", wrdDoc.ComputeStatistics(wdStatisticPages) 

Set RegExp = CreateObject("VBScript.RegExp")  
count = 0
With RegExp
.Global = True
.Pattern = "\d{8}"
For Each Match In RegExp.Execute(wrdApp.ActiveDocument.Range.Text)
Set myRange = wrdApp.ActiveDocument.Range   
Wscript.Echo Match & ", Page - " & myRange.Information(wdActiveEndPageNumber)
count = count + 1
wrdApp.Selection.GoTo 0
Next
End With

Wscript.Echo vbCrLf & "Found - " & count & " match(es)"  
wrdApp.Quit wdDoNotSaveChanges

 
#1
    59cobalt

    • Total Posts : 981
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:vbscript to search Word document for 8 digit numbers and display page number Sunday, December 18, 2011 6:56 AM (permalink)
    0
    john7a8
    I have a vbscript that opens a Word document and uses regexp to search for 8 digit numbers.  That part is working.  I now would like to display the page number of where the 8 digit number was found.  I am trying to use 'wdActiveEndPageNumber', but it is always printing the last page of the document as the page number.  Below is the script.  Can anyone tell me what I am going wrong?
    [...]
    Set myRange = wrdApp.ActiveDocument.Range
    Wscript.Echo Match & ", Page - " & myRange.Information(wdActiveEndPageNumber)
    ActiveDocument.Range is the entire document, so naturally the wdActiveEndPageNumber of that range is the number of the last page of the document. Also, you can't correlate the matches to page numbers, because the regular expression operates on the unformatted text of the document. Use Word's Find object instead:
    Const wdStory    = 6
    Const wdFindStop = 0
    Const wdActiveEndPageNumber = 3
    
    '...
    
    wrdApp.Selection.HomeKey wdStory
    
    With wrdApp.Selection.Find
     .ClearFormatting
     .Text             = "[0-9]{8}"
     .Replacement.Text = ""
     .Forward          = True
     .Wrap             = wdFindStop
     .Format           = False
     .MatchCase        = False
     .MatchWildcards   = True
    End With
    
    Do While wrdApp.Selection.Find.Execute
     WScript.Echo wrdApp.Selection.Text & ", Page - " & wrdApp.Selection.Information(wdActiveEndPageNumber)
    Loop

     
    #2
      wenbibi

      • Total Posts : 11
      • Scores: 0
      • Reward points : 0
      • Joined: 12/18/2011
      • Status: offline
      Re:vbscript to search Word document for 8 digit numbers and display page number Sunday, December 18, 2011 4:18 PM (permalink)
      0

      The climate in our country is very pleasant. Calgary Asian Escort It’s always warm in spring, hot in summer and cold in winter. Calgary Escort My favourite season is autumn, because it’s always warm in September and October. Calgary Asian Escorts It’s often cold in November. It’s certainly interesting. The other reason is the days are short and the nights are long. The sun rises late and sets early. I can do my favourite things in the evening. Calgary Escorts
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9