PDA

View Full Version : How to Loop each and every paragraph to get string



Rakesh
09-05-2014, 04:19 PM
Hi Guys,

I have tried to create a macro to count the Total tab of every paragraph and to make that as a string. But at last I have got the Total Tab count of Document and Stuck.
Please Let me know, How to loop each and every paragraph and to get the Total tab count as string.

The below coding for your reference.



Sub CountChars2()
Dim iCount(0 To 255) As Long
Dim i As Long
Dim j As Integer
Dim lCharCount As Long
Dim sDoc As String
Dim sTemp As String

' Initialize the array
For i = 0 To 255
iCount(i) = 0
Next i

' Assign document to a huge string
lCharCount = ActiveDocument.Characters.Count
sDoc = ActiveDocument.range(0, lCharCount)

' Fill the array
For i = 1 To Len(sDoc)
j = Asc(Mid(sDoc, i, 1))
iCount(j) = iCount(j) + 1
Next

' Only output code 9
For i = 9 To 0
sTemp = Chr(i)
If i < 32 Then sTemp = Trim(str(i))
sTemp = sTemp & Chr(9) & Trim(str(iCount(i)))
Next i
MsgBox iCount(i)
End Sub


Thanks in advance,
Rakesh

macropod
09-05-2014, 05:51 PM
So all you want is the count of tabs in the document? That could done as simply as:

Sub Demo()
With ActiveDocument.Range
MsgBox "There are " & _
Len(.Text) - Len(Replace(.Text, vbTab, vbNullString)) & _
" tabs in the body of this document."
End With
End Sub

snb
09-06-2014, 12:58 PM
Sub M_snb()
MsgBox "this document contains " & UBound(Split(ActiveDocument.Content, vbTab)) & " tabs"
End Sub

Rakesh
09-08-2014, 12:02 PM
Hi macropod & snb,

Thanks for your kind reply.
Both of your macro shows total tab count of the Document, But what I need is, total tab count of the paragraph one by one as string, So I will compare the string with another string.

Thanks,
Rakesh

macropod
09-08-2014, 11:27 PM
You could use something like:

Sub Demo()
Dim i As Long, StrRslt As String
StrRslt = "This document's paragraphs contain the following tab counts: " _
& vbCr & vbTab & "Paragraph" & vbTab & vbTab & "Tabs"
With ActiveDocument.Paragraphs
For i = 1 To .Count
StrRslt = StrRslt & vbCr & vbTab & i & vbTab & vbTab & UBound(Split(.Item(i).Range.Text, vbTab))
Next
End With
MsgBox StrRslt
End Sub
Do note, though, there's a limit to how much data a message box can display.

Rakesh
09-09-2014, 09:17 AM
Thanks a ton