how to track which section it's at

A

Associates

Hi,

I have a question i need to ask of you in regards to word 03. In the report
word document, I have a button called "insert picture". What it does is it
will bring up the insert picture windows dialog box and then prompt user for
the text message for the picture.

for example,

somewhere in the word report, we have something like as follows

Section 2. Light Bulb Prices

some texts ........................

picture
Figure 2.1 List of Light Bulb prices

some texts ........................


The number "2" in the Figure comes from the Section number. If the Section
is 3, then it should show Figure 3.1
Section 2 - this section is generated by Bullets and Numbering.

My question here is how do i know which Section it is now at?

I have the following code but it gives the wrong answer. The answer should
be 2 but it says 8 (which i think, is the page number)

Sub InsertPicture()

Dim srange As Range
Dim ssection As Section
Dim i As Long
i = Selection.Information(wdActiveEndSectionNumber)

Set srange = Selection.Sections(1).Range

srange.Collapse wdCollapseEnd

Set srange = ActiveDocument.Sections(i + 1).Range
srange.Collapse wdCollapseStart

Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Dialogs(wdDialogInsertPicture).Show

Selection.TypeParagraph

Selection.TypeText ("Figure " &
srange.Information(wdActiveEndSectionNumber) & "." & pictureCounter & " - " &
pictureText)
Selection.TypeParagraph
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub

Thank you in advance
 
J

Jay Freedman

You've got a bad case of reinventing the wheel here. Instead of trying to
hard-code the section number, insert a {SECTION} field.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
A

Associates

Hi Jay,

Thank you for your reply.

Sorry i think you have misunderstood my question. What i mean is when user
would like to add a picture, he/she would press button "insert picture" . The
"insert picture"
dialog box shows up and add the pic to the document. Not only that, it would
also add a label below the picture (label such as Figure 1.1 or 2.1 followed
by text)

Figure "1" or "2" depends on the Sections number. If the picture is placed
under Section 1, then it'd be Figure 1.1. If it's Section 3, it'd be Figure
3.1. So i'm not hard-coding the number in the figure but rather using codes
to track which Section it's at and print it on the report.

Hope my explanation helps you to understand my situation.

Thank you in advance
 
J

Jay Freedman

I did not misunderstand your question. When I said "hard-code the section
number" I meant that your macro is inserting a specific numeric character into
the label -- and in order to do that, the macro has to do some complicated
processing to decide what number that should be.

Instead, as the macro is constructing the label in the document, it should
insert a {SECTION} field. When the document's fields are updated, that will
automatically appear as the correct number of whatever section contains it.

In terms of actual code, replace your Selection.TypeText statement with this
sequence:

Selection.TypeText "Figure "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
Selection.TypeText "." & pictureCounter & " - " & pictureText
Selection.Paragraphs(1).Range.Fields.Update

Then you can remove all of the bits of code concerned with figuring out what
section you're in.

One more item: I assume this is only part of your code, because the variable
pictureText isn't declared or assigned a value.
 

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