#1
|
|||
|
|||
Get paragraph number from macro
Dear all,
I would want to retrieve from word macro the number associated to a paragraph in my document, but I am completely blocked on that point. I joined a small sample of a file with same style as the original. I already wrote all code needed to parse the info I need to collect from my document except that, when I iterate through the Document.Paragraphs collection, I would want to be able to retrieve the paragraph number (in this example 1.1.1.1) when reaching the paragraph of the sample. I tried the ListStyle, OutlineLevel, etc. properties, but with no success. Any help would be very appreciated ! Thanks in advance, Brice |
#2
|
||||
|
||||
You are probably going to have to do a bit of lateral thinking to grab this number from the autonumbered field in your document. The following macro should work for the paragraph at the cursor
Code:
Dim ofld As Field Dim orng As Range Set ofld = Selection.Paragraphs(1).Range.Fields(1) ofld.Unlink Set orng = Selection.Paragraphs(1).Range.Previous orng.Collapse 0 orng.MoveEndUntil Chr(9) orng.Select MsgBox orng.Text ActiveDocument.Undo
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thank you very much for your answer. I tested and it seems to work like a charm. But I have still two small questions :
First, I have big difficulties understanding your code. In particular, why do you make the unlink, previous and collapse stuff ? Second, imagine that now, I want to programmatically find the paragraph whose number is 1.2.3.4. By adapting your code, I can iterate through all paragraphs of document, extract the paragraph number with your code, and check if it matches. But is there a more efficient way to do this ? Thanks in advance. Regards, Brice |
#4
|
||||
|
||||
The number is created by a field { AUTONUMLGL }, but that field does not readily give up its result to VBA e.g.
Code:
Dim ofld As field For Each ofld In ActiveDocument.Fields If ofld.Type = wdFieldAutoNumLegal Then MsgBox ofld.Result End If Next ofld The example code therefore converts the field in the selected paragraph to plain text, which of course is readily accessible from VBA, which is what the range commands are about. It may well be inefficient but it works and is reasonably fast. To process all the paargraphs then the following should work - it does with your sample. Code:
Dim ofld As field Dim orng As Range Dim oPara As Paragraph Const strFind As String = "1.1.1." 'The number sequence to find For Each oPara In ActiveDocument.Range.Paragraphs If oPara.Range.Fields.Count > 0 Then Set ofld = oPara.Range.Fields(1) If ofld.Type = wdFieldAutoNumLegal Then ofld.Unlink Set orng = oPara.Range.Previous orng.Collapse 0 orng.MoveEndUntil Chr(9) If orng.Text = strFind Then orng.Select MsgBox orng.Text Exit For End If ActiveDocument.Undo End If End If Next oPara
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Thank you very much for those explanations.
Regards, Brice |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to find number of coma and then add that number of rows in word using macro? | PRA007 | Word VBA | 7 | 05-27-2015 10:45 PM |
Figure number for repeated use in paragraph | rahul4msoffice | Word | 1 | 05-01-2013 11:35 PM |
How place paragraph number in a text box? | peytontodd | Word | 2 | 12-13-2012 03:17 PM |
How set up a 'paragraph number'? | peytontodd | Word | 2 | 12-11-2012 06:55 PM |
How to identify the paragraph number you are on | SaneMan | Word VBA | 1 | 05-18-2011 05:43 AM |