A macro to get the current section number

  • Thread starter Vignesh Ganesan
  • Start date
V

Vignesh Ganesan

Hello All,

I'm trying to create a macro that will return the current section
number. I've read every article about this that I can find -

1.
http://groups.google.com/group/micr...gst&q=current+section+number#2b48bb877437540a

2.
http://groups.google.com/group/micr...gst&q=current+section+number#4334d3bd594d2fba

3. http://www.mvps.org/word/FAQs/MacrosVBA/GetIndexNoOfPara.htm

All of them, though helpful, don't quite work.

My document structure is this -

1. Section1
1.1. SubSection11
1.2. Subsection12
2. Section2
3. Section3
3.1 SubSection31
3.1.1 SubSubSection311
3.1.1.1 SubSubSubSection3111

What I want is the following - when I run my macro from within the
paragraphs of say SubSubSection311, the macro should return 3.1.1. The
articles I linked to above tell me how to count paragraphs, how to get
some representation of the "Section" I'm in, but none of them quite
work. I think the problem arises from the way Word handles "Sections"
- they really aren't sections are they ? They're headings, with List
thingies attached to them. So that's further complication to this...

Anyways, if anyone has any ideas of how to do this, I'd appreciate it
a lot.

Thanks,
Best,
Vignesh.
 
H

Helmut Weber

Hi Vignesh,

have a look at that one:

Sub Test400A()
With Selection.Bookmarks("\Headinglevel").Range
MsgBox .ListParagraphs(1).Range.ListFormat.ListString
End With
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
V

Vignesh Ganesan

Guten Tag Helmut !

Thanks for the macro. However, I believe I have mis-communicated my
requirement. Let me clarify -

My document structure is like this -
1. Section1
Test paragraphs

1.1. SubSection11 <--- cursor location 0
Test paragraphs <----- cursor location 1
Test paragraphs

1.2. Subsection12
Test paragraphs

2. Section2
Test paragraphs <---- cursor location 2

3. Section3
Test paragraphsTest paragraphsTest paragraphs
Test paragraphsTest paragraphs<>Test paragraphs <>----- cursor
location 3

3.1 SubSection31
Test paragraphs

3.1.1 SubSubSection311
<---- cursor location 4
Test paragraphsTest paragraphsTest paragraphs

<---- cursor location 5
3.1.1.1 SubSubSubSection3111
Test paragraphsTest paragraphs
Test paragraphsTest paragraphs

So now, the macro that you provided will return 1.1 for cursor
location 0. However, it doens't return anything for cursor locations
1-5. The macro I need should return the following -

Cursor Location 1 - 1.1
Cursor Location 2 - 2
Cursor Location 3 - 3
Cursor Location 4 - 3.1.1
Cursor Location 5 - 3.1.1

I'm sorry if I'm making this seem like a project ! But my intent is to
clarify the way MS Word deals with sections and headings. For example,
how do I find out what section cursor location 4 is in (Section
3.1.1) ? Do I look for the nearest heading bookmark/list object ?

Thanks a lot of the help so far,

Best,
Vignesh.
 
D

Doug Robbins - Word MVP

Sometimes, to provide assistance, it helps to know what you would be doing,
in this case, with the section number when you get it. There maybe a
completely different approach that will achieve the same thing.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
V

Vignesh Ganesan

Sorry, I should have probably done that earlier. Thanks for pointing
that out Doug.

Here's my usage - I'm writing a requirement document for a software
product. For that, I've got a macro that generates a unique number for
each requirement. The guys I'm working with on this product like the
unique ID which unlike lists doesn't change value (due to list
reordering) when you insert a requirement in between two existing
requirements. But as the document grows larger, they're starting to
find it difficult to track a requirement number (which is of the form
REQ-ID-345 for example) to a particular section in the specification.
So the reason for asking these questions is to figure out how I can
get the macro to generate the requirement id as REQ-ID-3.1.1-345,
where 3.1.1 is the section number.

Thanks,
Best,
Vignesh.
 
H

Helmut Weber

Hi Vignesh,

if the macro I provided will return 1.1 for cursor
location 0, then you are using some kind of automatic numbering.

How about that one:

Sub Test400B()
Dim rTmp1 As Range
Dim rtmp2 As Range
Set rTmp1 = Selection.Range
Set rtmp2 = Selection.Range
While rTmp1.ListParagraphs.Count = 0
rTmp1.MoveStart Unit:=wdParagraph, Count:=-1
' rTmp1.Select ' for testing
Wend
MsgBox rTmp1.ListParagraphs(1).Range.ListFormat.ListString
rtmp2.Select
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
B

Barry Schwarz

Your original request apparently asked for section number. Section
has a particular meaning in Word (the area between two section breaks)
different from your use. What you call a section is a numbered
paragraph in Word terminology. Hopefully you are using styles to
automatically number the paragraphs to handle the later insertions. A
cross-reference will provide the number of the paragraph.

I can't figure out how adding the paragraph number to the requirement
will help solve the tracking problem since what was REQ-ID-3.1.1-345
may end up in paragraph 3.2.4 after insertions.

Sorry, I should have probably done that earlier. Thanks for pointing
that out Doug.

Here's my usage - I'm writing a requirement document for a software
product. For that, I've got a macro that generates a unique number for
each requirement. The guys I'm working with on this product like the
unique ID which unlike lists doesn't change value (due to list
reordering) when you insert a requirement in between two existing
requirements. But as the document grows larger, they're starting to
find it difficult to track a requirement number (which is of the form
REQ-ID-345 for example) to a particular section in the specification.
So the reason for asking these questions is to figure out how I can
get the macro to generate the requirement id as REQ-ID-3.1.1-345,
where 3.1.1 is the section number.

Thanks,
Best,
Vignesh.


Remove del for email
 
V

Vignesh Ganesan

Hi,

Thanks a lot ! That solution worked ! BUT, considering what Barry's
pointed out in the following post, I think I'm going to have to
rethink my strategy for this. But thanks a lot anyway !

Best,
Vignesh.
 
V

Vignesh Ganesan

Damnations !!! You're right... I hadn't thought of that ! Any
suggestions ? Is there a way I can insert a cross-reference as you
suggested ? This would get updated automatically, right ?

Thanks for pointing that out !

Best,
Vignesh.
 
B

Barry Schwarz

You could record a macro that inserts a cross reference and then check
the Help for the InsertCrossReference Method in VBA help to customize
the macro.

Damnations !!! You're right... I hadn't thought of that ! Any
suggestions ? Is there a way I can insert a cross-reference as you
suggested ? This would get updated automatically, right ?

Thanks for pointing that out !

Best,
Vignesh.


Remove del for email
 
A

Alicia Napier

Does anyone have a macro that counts sections in a Word document and does more than just displays the total sections in a message box? I need to grab the value (number of sections) and somehow use that to indicate how many times to copy and paste the information from the footer in the first section to the rest of the secionts. Any help is appreciated!
-Alicia
 
D

Doug Robbins - Word MVP

Two replies have now been posted to your later post. One telling you how to
do and the other providing you with the code for a macro that will do it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top