PDA

View Full Version : index number of selected paragraphs



suji
12-17-2008, 06:46 AM
Hi,

Can someone help me on how to find the index numbers of selected paragraphs.

Thanks
Suji

fumei
12-17-2008, 12:28 PM
Index numbers? Please explain further what you want.

Do you mean if you select the fourth and fifth paragraphs in a document, you want to get the numbers 4 and 5?

This would be very tricky to do.

suji
12-17-2008, 11:41 PM
Exactly, but not necessarily in the serial order ie even if I select say paras 4, 6, 9 etc.

TonyJollans
12-18-2008, 01:42 AM
If you select disjoint paragraphs you will not be able to work with them all in VBA - in your example, you will only see paragraph 4.

What do you want to do with the paragraph numbers?

suji
12-18-2008, 06:18 AM
I have attached a sample of my file. What I would to do on this is when I select a few paragraphs and run macro, I should get a message with total of columns 5 and 6 of the selected paras. Column 05 is weight and column 06 is volume.

Eg :- If I select paras 4,5,7 and run macro, I should get a msgbox with sum of values in paras 4,5,7 of column 5 and column 6.

Thanks
Suji

fumei
12-18-2008, 10:39 AM
1. your sample file does not have any columns.

2. this should be in Excel, as this is a poor use of Word.

3. Tony is correct about non-contiguous Selections (e.g. selecting 4, 5, 7)

4. what does your request to get information from each paragraph have to do with your original question about index number?

suji
12-18-2008, 11:07 AM
Dear Fumei,

1. These are system generated fixed width data.
2. I know but unfortunately these are system generated as documents.
3. How about if the selection is contigious.
4. My intention was to get the weight and volume value for each selected row by using the mid function.
eg: Mid(ActiveDocument.Paragraphs(25), 30, 8) to get weight and Mid(ActiveDocument.Paragraphs(25), 38, 10) to get the volume of row 25. Forgive me if my logic or codes are stupid since I am a totally new to this.

Thanks
Suji

fumei
12-18-2008, 12:37 PM
No, no. No one is saying anything is stupid. We are just trying to understand what is going on. Explaining that you want to use Mid to get the information from paragraphs by index number tells us. You need to explain things fully.

Given that, I still fail to see why you need the document paragraph index number. Why not use the Selection, as that is what you are working with? Like this (demo attached):
Option Explicit

Sub whatever()
Dim j As Long
Dim msg As String
Dim Weight As Double
Dim Vol As Double

For j = 1 To Selection.Paragraphs.Count
msg = msg & "Weight: " & _
Mid(Selection.Paragraphs(j), 30, 8) & _
vbTab & _
"Vol: " & _
Mid(Selection.Paragraphs(j), 38, 10) & _
vbCrLf
Weight = Weight + CStr(Mid(Selection.Paragraphs(j), 30, 8))
Vol = Vol + CStr(Mid(Selection.Paragraphs(j), 38, 10))
Next
MsgBox msg & vbCrLf & _
"Total weight: " & vbTab & Weight & _
vbCrLf & _
"Total vol: " & vbTab & Vol

End Sub


Select some paragraphs - and they MUST include a paragraph mark - and click "Get Weight/Volume" on the top toolbar.

You will get something like:
Weight: 227.0 Vol: 1.4
Weight: 274.0 Vol: 0.94815

Total weight: 501
Total vol: 2.34815

This uses the Selection paragraph index number - essentially looping through the paragraph collection of the Selection. You do not need to know (as far as I can see) the document paragraph index number at all.

Now, as a further test, first try selecting contiguous paragraphs. You can do this in the GUI (what you see on screen). Say select paragraphs 5 and 6, and then click "ParaCount" on the top toolbar.

You get: "Paragraph.Count = 2"

Two paragraphs in the Selection, right?

Now, using Ctrl, select a third non-contiguous paragraph, say paragraph 8...whatever.

You can see in the GUI (on screen) that three paragraphs are selected. Now click "ParaCount" again. Result?

"Paragraph.Count = 1"

Why?

Because while VBA Help does state that the Paragraph Collection Object is:

A collection of Paragraph objects in a selection, range, or document.and that is true, but not totally true.

When you use:

Selection.Paragraphs.Count

The actual REAL (that is, as interpreted by VBA) syntax is:

Selection.Range.Paragraphs.Count

And Range does NOT see non-contiguous regions.

So while you could have 13 paragraphs "selected", if the LAST paragraph is non-contiguous to the others...the Selection.Paragraph.Count = 1.

Note, for the demo, I used your original document, but removed those extraneous spaces at the end of each paragraph, plus made it shorter. The number of paragraphs I left are more than enough to demonstrate.

Again, you must have a paragraph mark in whatever you select. However, you could select, say, paragraph 5 (including its paragraph mark) and part of paragraph 6. That would still work, as long as the part of the other paragraph DOES include enough to cover numerically the Mid function values.

suji
12-18-2008, 01:15 PM
Fumei,

Thanks a ton for the detailed explanation and solution. Actually I didnot know that I could get information from paragraph by using selection.paragraphs .... I thought ActiveDocument.Paragraphs was the only option and thats why I wanted to get the paragraph index.

fumei
12-18-2008, 01:25 PM
Hopefully this will help you move things along.

Good luck.

I would just like to add again that trying to give a full explanation helps us help you. Here is your original post.

"Can someone help me on how to find the index numbers of selected paragraphs."

As a suggestion, here is a possible better way to ask for what you were really wanting to ask about.

"I need to get two separate pieces of text from each paragraph of some text I have selected. For example, I have selected three paragraphs and I want to get the two pieces of text from each paragraph, like this:

(Paragraph)I need to get this text and in the same paragraph this text.
(Paragraph)I need to get this text and in the same paragraph this text.
(Paragraph)I need to get this text and in the same paragraph this text.

The bolding above is just to highlight the pieces I want. The actual text is not bolded.

The two pieces of text are at the same location in each paragraph, and I am thinking of using the Mid function to get them.

How do I go through each paragraph in my Selection to get those pieces?

The pieces of each paragraphs are numbers, and I also want to add them together. Any suggestions would be helpful. Thanks."


That tells us the real story.

1. you want to get information out of each paragraph in your selection.
2. the position in each paragraph is the same.
3. you give an example to show us.
4. you clearly state the bolding in the example is just to demonstrate, and is NOT a factor. Therefore, we are not going to ask if it may be easier to get the text pieces by looking for bolding.
5. you wish to perform numeric operations on the information

My point being is that in my demo I did not "find" the index number of each paragraph. I used the index number as a variable - j in this case. What the actual index number is does not matter.

For j = 1 To Selection.Paragraphs.Count



It does not matter if there is 3 paragraphs, or 23. Therefore the actual index number does not matter...you just want to go through ALL of them.

suji
12-18-2008, 01:51 PM
Thankyou Fumei. Will make all effort to be more precise in my queries in future.

fumei
12-22-2008, 12:12 PM
Good. Just as long as you are not taking me mentioning it as being critical of you personally. I do not wish that. I wrote that for one reason...so you can get the best assistance possible from the people here. The more you tell us, the easier it is to offer something back.