Monday, April 09, 2012 1:26 PM
How to get current line number from Word using Macros. Just to be clear I am not asking for that function
wdFirstCharacterLineNumber returns only Line Number for current page and is NOT taking into account paragraphs that are Suppressed from counting.
1. Document has many sections (let's assume more than 3)
2. Every section has more than one page
3. Line Numbering is switched on with the following options: Start at 1; Count by 5; Restart at each section.
4. Some paragraphs has "Suppress for Current Paragraph" option switched on and they should not be counted
On the PrintView view I see proper line numbering. E.g. if on the Second section I have on first page 3 paragraphs each has let's say 2 lines and the second paragraph is Suppressed, then when cursor is in the last line I see line number 4 (not 6 because one 2 lines paragraph is suppressed). If the same text is on the second page (same Section), then when cursor is on the last line I see line number 8.
The problem is that if I want to get this line number by macro with wdFirstCharacterLineNumber on the first page of the section I am getting 6 (because it counts also Suppressed paragraph) but what is even worse on the second page I also am getting 6 because this is page relative function.
Any idea how to get line number that will follow rules 1-4 the same as in the PrintView view?
Wednesday, April 11, 2012 8:46 AMModerator
Thanks for posting in the MSDN Forum.
This is a issue regard to Word VBA. We will move this thread to Word dev forum for better support.
Have a good day,
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- Edited by Tom_Xu_WXModerator Thursday, April 12, 2012 1:21 AM
Friday, April 13, 2012 4:39 AM
Thanks for reaching out to us, I have a few questions for you :
- What build and bits are you running for Word e.g. Word 2007 32bit 12.XXXX.XXXX SP3?
- What OS version and build are you running Word on?
- Do you think by any chance you can provide us with a small sample macro enabled document/template that reproduces the current outcome you are having (with Actual results and steps to reproduce the results when using the sample file and your expected/desired results if need be.). You can make that available to me by uploading it to a skydrive.
Thursday, April 19, 2012 11:32 PM
Did some research and based on my understanding of what you have mentioned we gathered the following:
- The line numbers you see on the status bar on the bottom of the page are initialized at the beginning of every page.
-We know the Selection.Information(wdFirstCharacterLineNumber) should take into account paragraphs that are suppressed. I am not sure why when you use wdFirstCharacterLineNumber this is not taking into account paragraphs that are suppressed, we also could not reproduce that issue. I would advise you to test this with any other add-ins you may have installed - disabled.
-The wdFirstCharacterLineNumber is also supposed to Return the character position of the first character in the specified selection or range. If the selection or range is collapsed, the character number immediately to the right of the range or selection is returned (this is the same as the character column number displayed in the status bar after "Col"). Please see msdn article:
-Finally given the above which of the following line numbers would you like to return from your selection in the document?
- Line number with count starting from the beginning of the first line of each section?
- Line number with count starting from the beginning of the first line of each Page?
- Line number with count starting from the beginning of the first line in that document?
Thanks for your time and hope this information is helpful.
Thursday, June 28, 2012 1:14 PM
Thanks for your wonderful elaboration. I get the usage of the wdFirstCharacterLineNumber before, but I can't get the line number with count starting from the beginning of the first line in whole document.
I have tried the "range.ComputeStatistics(wdStatisticLines)" too. Set a new range from the start of the document to the first character of the selection, and then call the "range.ComputeStatistics(wdStatisticLines)". But it returns the line number including lines in tables or orther things which are not counted in the line number displayed on the left margin. It's different from what we have seen on page display appearance.
So now I only can count the line in every pages before and make the summary of them. it's very slow and stupid...
Could you help me giving a breif and efficient method to get the
Line number with count starting from the beginning of the first line in that document?
- Edited by Charles WangChao Friday, June 29, 2012 4:37 AM