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

Posted on 2001-08-19
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
Question by:seabear
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

Expert Comment

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, _
            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
      IsLastCharParagraph = True
      If Not blnTrimParaMark = True Then
            Exit Function
            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

LVL 39

Expert Comment

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

LVL 22

Expert Comment

ID: 6405671

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Expert Comment

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.
LVL 16

Expert Comment

ID: 6408462

Author Comment

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

Expert Comment

ID: 6408752

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.

LVL 22

Accepted Solution

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

Expert Comment

ID: 6417419

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


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 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