Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

current position within document 1

Status
Not open for further replies.
Jan 28, 2003
149
0
0
GB
Hi,

Can someone please tell me how I can determine the page/line number of the selection, and also the total number of pages/lines within the document (as displayed in Document Properties, Statistics).

Thanks in advance.


B.M.
 
The Information Property is used to return most most if not all of the information found in the Stats dialog. the example line of code returns the exact line number where my insertion point is:

Dim intStart as Integer
intStart = Selection.Information(wdFirstCharacterLineNumber)

To determine the number of words, Paragraphs, Sentences, you can use the respective object collections Count method. For example:

Dim intPara As Integer
intPara = ActiveDocument.Paragraphs.Count

I Teach as use both techniques. I hope you can run with this one.

Jamie

 
Hi BlueMonkey,

To get the Statistics which Word shows under File > Properties use the BuiltInDocumentProperties Property:

Code:
ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
Code:
 ' Number of lines in document
Code:
ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Code:
 ' Number of pages in document

To find the current page number and line number use the Information property:

Code:
Selection.Information(wdActiveEndPageNumber)
Code:
 ' Absolute Page number
Code:
Selection.Information(wdActiveEndAdjustedPageNumber)
Code:
 ' Page number according to any adjustments you have made
Code:
Selection.Information(wdFirstCharacterLineNumber)
Code:
 ' Line number
Code:
within page

Be aware that Word doesn't seem to like the concept of Lines very much generally. The document property value seems fairly good, but the 'information' value is reset every page. Also, if you use line numbering in Page Setup it does not include lines in tables within its count so you can get different values depending on what you ask for.

Its also worth noting that the values can be affected by the printer you have selected and repagination when printing may change them.

Enjoy,
Tony
 
Thanks for both of the answers, both of which have put me off the solution I'm trying to code!

Can I provide you with the scenario and ask for your ideas on a solution?

I have an index as a txt file that ultimately I am converting to HTML format, to fully utilise hyperlinks and the like. The code I am creating whizzes thru the document and remove unneeded information. This works fine, but I am having trouble stopping the macro when the end of the document is reached.

Is there an easy way to determine if you are at the end of the document?

Cheers again

B.M.
 
If I understand the question, you are reading from a .txt file.

I use the following loop:

Do while EOF(FreeFile) <> True
Magic happens here
Loop

The EOF is EndOfFile. The loop will continue until it reaches the end of the txt file.

 
The txt file is not a &quot;Input File&quot; as such, it's simply an automated output from another application

B.M.
 
Hi B.M.,

It really depends what you are doing and how you are going through your document but it might help you to know that Selection.End is the character position at the end of your current selection and ActiveDocument.Range.End is the character position at the end of your document.

If you are using, for example, Find, to search for things you can tell it to stop at the end of your document. So, as I say, it rather depends. If you want any more help post back with some code or a bit more info about how you process your document.

Enjoy,
Tony
 
Tony,

I'm searching thru' a 500 page document and replacing instances with a variable - ie I can't use a &quot;global&quot; find and replace. The problem I've had is knowing when I at or nearly at the end of the document. The final instance of the text I'm searching for occurs 7 or 8 lines from the end of the document, so I was trying to determine if the current line number is within 4 or so lines from the very end - unless you can suggest a better close.

B.M.
 
Hi B.M.,

Are you using Word's Find (i.e. Selection.Find.Execute) to search? If so you should be able to say set .Find.Wrap = wdFindStop before the Exceute and then after the Execute check .Find.Found (True or False) before deciding what to do next.

if you are searching in some other way, please post a bit of code and I'll take a look.

Enjoy,
tony
 
Tony

I've elected not to send the code at this time, because I can probably explain what it does better. If you think it'll help I'll post next time.

I have a document output from PVCS, listing details of several thousand documents. The details are exported in a vertical list, along the lines of:
FileName: xxx
Title: xxx
Description:
this is the description here.

Once word has done it's bits it goes on to access. The problem I've got in word is that Description field, which for some bizarre reason PVCS exports on a line to itself.

I've created a small routine that, amongst other things, Puts the actual description onto the same line as the description heading. I can't use a standard Find and Replace of Description:^p to fix the whole problem because the description field often spills onto more than one line, seperated by paragraph marks.

So, the idea is I perform a loop of somekind without knowing how many loops it will perform. A DO UNTIL loop sounds like the answer if there's a &quot;UNTIL&quot; criteria that will stop when the last Description has been changed. Can you return if a find and replace was succesful? Is there anyway of determining how many occurances of the word there is? Can you determine the current line number? Can you return if a find and replace was succesful?

Cheers and thanks a million


B.M.
 
Hi B.M.,

It's more than ten years since I've worked with PVCS - so I can't offer anything on that front. But in Word I'd really try and get away from line numbers. Just let Word tell you when the job is finished instead of trying to second guess it yourself. In the absence of your code, here's some code of mine [smile]. This should approximate to what you want. Note the statements (in red) from my previous post which control the loop end.

Code:
Dim DescriptionStart As Long, DescriptionEnd As Long

With Selection.Find
Code:
' Set some defaults for searches
Code:
    .Replacement.Text = &quot;&quot;
    .Forward = True
Code:
' Look forward through document
Code:
Code:
.Wrap = wdFindStop
Code:
Code:
' Stop searches at end of doc
Code:
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    
End With
    
Application.ScreenUpdating = False

Selection.HomeKey Unit:=wdStory
Code:
' Top of document
Code:
Do: With Selection
    
    .Find.Execute FindText:=&quot;Description:^p&quot;
Code:
' Find (next) Description tag
Code:
    If Not
Code:
.Find.Found
Code:
 Then Exit Do
Code:
' No more descriptions, so quit
Code:
    DescriptionStart = .End - 1
Code:
' Remember pos'n of para mark
Code:
    .Collapse wdCollapseEnd
Code:
' Ready for next find
Code:
    .Find.Execute FindText:=&quot;FileName: &quot;
Code:
' Find end of description
Code:
    If .Find.Found Then
Code:
' If found ...
Code:
        DescriptionEnd = .start - 1
Code:
' ... remember pos before it
Code:
    Else
Code:
' if not ...
Code:
        DescriptionEnd = ActiveDocument.Range.End
Code:
' ... remember end of document
Code:
    End If
Code:
' Replace all paragraph marks in Description text with spaces
Code:
Code:
' You’ll probably want to do something else here
Code:
    ActiveDocument.Range(DescriptionStart, DescriptionEnd).Find.Execute _
            FindText:=&quot;^p&quot;, ReplaceWith:=&quot; &quot;, Replace:=wdReplaceAll
    
    .Collapse wdCollapseEnd
Code:
' Ready for next find
Code:
End With: Loop
Code:
' On a carousel
Code:
Application.ScreenUpdating = True

Enjoy,
Tony
 
Cheers Tony,

Just glancing thru this (it's a work thing and I don't have the file at home), I suspect the If Not .Find.Found and the Exit Do will crack what I'm trying to do.

FYI - I want to replace the paragraph marks with spaces - just as you thought I wouldn't! it keeps things simple and it's easy to import and parse into Access that way. Thanks for your offer of help on PVCS, but it's not needed - I just find it's behaviour on this unusual.

I'll let you know on Monday.

Cheers

C

B.M.
 
Tony,

This has indeed worked blindingly well. Many thanks for all your help. If I could give you something more than this star, I would.

Cheers again.

BM

B.M.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top