Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBA Word - How do I loop through a doc changing each paragraph?

Posted on 2001-08-19
9
1,311 Views
Last Modified: 2010-10-05
I would like to take a simple Word document and loop through from the start to the end and for each paragraph that I find I want to set the font color for the paragraph to Blue AND insert a blank line above the paragraph.

I am actually going to do some more complex formatting but the key thing I do not know is how to loop through a document repeating a set of actions for each paragraph.

Thanks in advance - Dan
0
Comment
Question by:seabear
9 Comments
 
LVL 1

Expert Comment

by:sanjeevjain1973
ID: 6404845

You Can read Paragraph of word doucument using VB. after read parapgraph u can change color, font... For More detail u cause use paragraph object in MSDN help.

Following are procedure to read the range & paragraph.

Public Sub GetRangeExample()
   ' This example shows how the Range method and the Range
   ' property both return the same characters.
   
   Dim rngRangeMethod        As Word.Range
   Dim rngRangeProperty      As Word.Range
   
   With ActiveDocument
      If .Sentences.Count >= 2 Then
            Set rngRangeMethod = .Range(.Sentences(2).Start, _
                .Sentences(2).End)
            Set rngRangeProperty = .Sentences(2)
      End If
   End With
   
   Debug.Print rngRangeMethod.Text
   Debug.Print rngRangeProperty.Text
End Sub





Function IsLastCharParagraph(ByRef rngTextRange As Word.Range, _
                            Optional blnTrimParaMark As Boolean = _
                            False) As Boolean
   ' This procedure accepts a character, word, sentence, or
   ' paragraph Range object as the first argument and returns True
   ' if the last character in the range is a paragraph mark, and
   ' False if it is not. The procedure also accepts an optional
   ' Boolean argument that specifies whether the Range object
   ' should be changed to eliminate the paragraph mark if it
   ' exists. When the blnTrimParaMark argument is True, this
   ' procedure calls itself recursively to strip off all trailing
   ' paragraph marks.
   Dim strLastChar As String
   
   strLastChar = Right$(rngTextRange.Text, 1)
   If InStr(strLastChar, Chr$(13)) = 0 Then
      IsLastCharParagraph = False
      Exit Function
   Else
      IsLastCharParagraph = True
      If Not blnTrimParaMark = True Then
            Exit Function
      Else
         Do
            rngTextRange.SetRange rngTextRange.Start, _
               rngTextRange.Start + rngTextRange.Characters.Count - 1
            Call IsLastCharParagraph(rngTextRange, True)
         Loop While InStr(rngTextRange.Text, Chr$(13)) <> 0
      End If
   End If
End Function



Sanjeev
0
 
LVL 39

Expert Comment

by:appari
ID: 6404846
in VB take reference of Microsoft word object library then you can create word object and open any existing document and format it

for example after opening the document to change properties of first para use following code.
in the same way you can loop through all paragraphs in the document, ActiveDocument.Paragraphs.Count gives you the number of paragraphs in the document.

Dim rngPara As Range

Set rngPara = ActiveDocument.Paragraphs(1).Range
With rngPara
   .Bold = True
   .ParagraphFormat.Alignment = wdAlignParagraphCenter
   .Font.Name = "Arial"
End With


0
 
LVL 22

Expert Comment

by:ture
ID: 6405671
seaber,

Something like this?

Sub LoopThroughParagraphs()
  Dim p As Paragraph
  For Each p In ActiveDocument.Paragraphs
    p.Range.InsertBefore vbCr
    p.Range.Font.Color = wdColorBlue
  Next p
End Sub

Ture Magnusson
Karlstad, Sweden
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:Sorklin
ID: 6407620
I've been doing lots of VBA for word programming and Ture's solution is the best.  I would disregard sanjeevjain1973's solution because its re-inventing the wheel at best and buggy at worst.

Word already has made a paragraph an object.  Its easiest to use it as Ture has.

If you need to run it from VB then reference the Word library like appari talks about.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6408462
Learning...
0
 

Author Comment

by:seabear
ID: 6408489
Nice clean code - thanks very much Ture - Dan
0
 
LVL 22

Expert Comment

by:ture
ID: 6408752
Dan,

You are welcome!

I assume that you forgot to accept my comment as an answer, so I upgrade to answer now.

Sorklin - thanks for your comment. I'm glad that you like my approach also.

/Ture
0
 
LVL 22

Accepted Solution

by:
ture earned 100 total points
ID: 6408757
Ooops. I forgot to check "Answer"...
0
 
LVL 22

Expert Comment

by:ture
ID: 6417419
Dan,

if you are happy with my answer, please do not forget to accept and grade it.

/Ture
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 59
Search combo error "Data Type Mismatch in Criteria Expression" 2 68
TT Auto Dashboard 13 95
Send outlook email from VBS Script 2 42
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question