Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-29-2015, 08:05 AM
brice brice is offline Get paragraph number from macro Windows 7 64bit Get paragraph number from macro Office 2010 32bit
Novice
Get paragraph number from macro
 
Join Date: Jun 2015
Posts: 3
brice is on a distinguished road
Default 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
Attached Files
File Type: doc example.doc (64.0 KB, 15 views)
Reply With Quote
  #2  
Old 06-29-2015, 10:49 PM
gmayor's Avatar
gmayor gmayor is offline Get paragraph number from macro Windows 7 64bit Get paragraph number from macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 06-30-2015, 02:08 AM
brice brice is offline Get paragraph number from macro Windows 7 64bit Get paragraph number from macro Office 2010 32bit
Novice
Get paragraph number from macro
 
Join Date: Jun 2015
Posts: 3
brice is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 06-30-2015, 03:08 AM
gmayor's Avatar
gmayor gmayor is offline Get paragraph number from macro Windows 7 64bit Get paragraph number from macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
shows no values

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
Reply With Quote
  #5  
Old 06-30-2015, 03:29 AM
brice brice is offline Get paragraph number from macro Windows 7 64bit Get paragraph number from macro Office 2010 32bit
Novice
Get paragraph number from macro
 
Join Date: Jun 2015
Posts: 3
brice is on a distinguished road
Default

Thank you very much for those explanations.

Regards,

Brice
Reply With Quote
Reply

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
Get paragraph number from macro 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
Get paragraph number from macro How set up a 'paragraph number'? peytontodd Word 2 12-11-2012 06:55 PM
Get paragraph number from macro How to identify the paragraph number you are on SaneMan Word VBA 1 05-18-2011 05:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:33 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