Selecting individual paragraphs of specific length

R

Rickeh

Hi im wrting a program in C#.NET and I'm using Word Automation to
manipulate a Word document and its paragraphs. Basically this is what I
have so far:

<existing code to open a word doc>

<button click>
Word.Document oWordDoc = oWordApp.ActiveDocument;
Word.Paragraphs oWordDocp = oWordDoc.Paragraphs;

For (int i = 1; i <= oWordDocp.Count; i++)
{

label1.Text = oWordDocp.Range.Text;

}

The code counts the number of paragraphs and only prints the last
paragraph within the document. What it's meant to do is step through
the paragraphs (the For statement).
I will work on it a little later to try to increment each paragraph 1
by 1 and display the text in each paragraph within the C# program. Also
I have noticed that every white space in-between paragraphs is also
counted as a paragraph, any way around this? My overall aim is to count
the number of words within a paragraph and if they meet the requirement
output/copy the text if not move onto the next paragraph (while
excluding white spaces). I'm guessing I will be using a For Loop
similar to the one above or maybe a Do While. I know that this program
is in C#.NET but im happy for help in VBA. (cross language programming
through .NET shouldnt be a problem)
Any ideas?
 
H

Helmut Weber

Hi Rickeh,

I am assuming you want something along these lines:

Dim oPrg As Paragraph
Dim lWrd As Long ' words count
Dim lChr As Long ' characters count
Dim sAvr As Single ' average word lenght

For Each oPrg In ActiveDocument.Paragraphs
With oPrg.Range
lChr = Len(.Text)
If lChr > 1 Then ' whitespace ???
lWrd = .Words.Count
sAvr = lChr / lWrd
MsgBox "Average: " & Format(sAvr, "#.00") _
& chr(13) & .Text
End If
End With
Next

I don't know, what you mean with withespace here.
I thought, maybe you mean empty paragraphs.

By the way, IMHO, there is never anything
_between_ paragraphs in Word.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg

Rickeh

Helmut showed you one way. Another is to move the range along the
document.

Public Sub ScratchMacro()
Dim aPara As Word.Range
With ActiveDocument.StoryRanges(wdMainTextStory)
Set aPara = .Paragraphs(1).Range
Do
If aPara.Words.Count > 3 Then 'See Note
MsgBox aPara.Text
aPara.Collapse wdCollapseEnd
aPara.MoveEnd wdParagraph, 1
Else
aPara.Collapse wdCollapseEnd
aPara.MoveEnd wdParagraph, 1
End If
Loop Until aPara.End = .End
If aPara.Words.Count > 3 Then
MsgBox aPara.Text
End If
End With
Set aPara = Nothing
'Note - Word counts words, punctuation, and the paragraph mark as a
word.
'So "Hello world!" as a single paragraph is counted as 4 words.
End Sub

For the "white space" or empty paragraphs, see:
http://gregmaxey.mvps.org/Count_Lines_of_Text.htm
 
G

Greg

Hmm. In view of the note in my last post, the following might be
better:

Public Sub ScratchMacro()
Dim aPara As Word.Range
With ActiveDocument.StoryRanges(wdMainTextStory)
Set aPara = .Paragraphs(1).Range
Do
If aPara.ComputeStatistics(wdStatisticWords) > 3 Then 'See Note
MsgBox aPara.Text
aPara.Collapse wdCollapseEnd
aPara.MoveEnd wdParagraph, 1
Else
aPara.Collapse wdCollapseEnd
aPara.MoveEnd wdParagraph, 1
End If
Loop Until aPara.End = .End
If aPara.ComputeStatistics(wdStatisticWords) > 3 Then
MsgBox aPara.Text
End If
End With
Set aPara = Nothing
End Sub
 

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