Solved

MS Word - Finding Text within Table to Change Style

Posted on 2009-05-14
4
338 Views
Last Modified: 2012-05-07
I need a macro to search through a Word document for specific text located in a table, then tab to the next cell and change the Style of the text in that cell to a specified Style.  I've been trying to use various code snippets I've found on the Web and I'm getting more and more confused about how to combine With - End With structures with Do While loops to get the result I'm after.  

The code below is my latest attempt. It finds the first occurence of the search text, tabs to the next cell and successfully changes the Style of the text in that cell.  However, on the next iteration through the loop, the Find does not work and the following commands start creating more rows in the table and setting them to the new Style.  Can someone please help me troubleshoot this code, or recommend an alternate approach?  Any recommendations of tutorials on using these structures?
Sub FindTaskTitle()
   Dim FindRange As Range
   Set FindRange = ActiveDocument.Range
   With FindRange.Find
       .ClearFormatting
       .Forward = True
       .Format = False
       .Wrap = wdFindStop
       .MatchWildcards = False
       .Text = "Task Title:"
     Do While .Execute
         With Selection.Find
           .Text = "Task Title:"
           .Replacement.Text = ""
           .Forward = True
           .Execute
         End With
         Selection.MoveRight Unit:=wdCell
         Selection.Style = ActiveDocument.Styles("BOE Title")
     Loop
   End With
End Sub

Open in new window

Sample-Word-Doc2.doc
0
Comment
Question by:srauler58
[X]
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
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24393209
It think that you can get away with something a lot simpler:
Sub FindTaskTitle()
   With ActiveDocument.Range.Find
       .Text = "Task Title:"
       .Replacement.Style = "BOE Title"
       .Execute Replace:=wdReplaceAll
   End With
End Sub

Open in new window

0
 

Author Comment

by:srauler58
ID: 24394950
Thanks, Graham, for the reply, but it's not quite that simple.  Here's what I'm trying to get the script to do:
   1) Find the first occurence of "Task Title"
   2) Move right to the adjacent cell in the table
   3) Change the Style of the text in that cell, which varies from page to page,  to "BOE Title"
   4) Find the next occurence of "Task Title" on a subsequent page and repeat the above actions
   5) Continue executing until no more occurences of "Task Title" are found

The problem with my current approach is, after finding the first occurence of the "Task Title" text and changing the style of the text in that cell, the script doesn't do another Find immediately; it loops back around, does another MoveRight (which adds an unwanted row under the one I just modified), and then changes the Style of both cells in the new row to "BOE Title".  After it does this, then it will find the next "Task Title" on a subsequent page and do the same thing including adding an unwanted row.

I've got a logic problem and my lack of familiarity with the MS Word object model is frustrating me as I can't figure out whether the issue is that I'm doing the tasks within a table, or if it's just a loop and execute issue. I'm confused about where to put the ".execute" in the Do While loop.  I've been studying this for days and still haven't figured out where I'm going wrong.

I'm attaching an updated sample file that shows the results of running my script.  In this case, the script made the required style change on the first three pages while adding unwanted rows, then stalled before making the style change on the fourth page.  Help!
Sample-Word-Doc3.doc
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 24398948
I believe that my macro does all that you describe in steps 1 to 5. It does not check that the text is in a cell and does not distinguish between pages, but I don't think those checks are necessary.

There are times when you might want to do repeated individual Finds, and run some code after each one, but that does not seem to be necessary here. That is fortunate, because it is difficult to start a Find range inside a table, excluding previously searched parts. One such problem is discussed here.

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_21728321.html
0
 

Author Comment

by:srauler58
ID: 24400560
Graham: Thanks for the followup... The link you provided in your last response worked wonders!  I took the last solution that Spencer Steel came up with and modified it as shown below in the code snippet... voila!  I've run it against the attached sample file and a longer file containing actual data and it worked great in both cases.

Thanks for hanging in there on this one; you wouldn't believe how frustrated I was getting about something I thought should be an easy task! (Well, maybe you would... you work with Microsoft products every day!).  Thanks again, and a tip of the hat to Spencer Steel, wherever he is!

- Steve R.
Sub SetBOETitleStyle()
    Set rng = ActiveDocument.Content
    With rng.Find
        .Text = "Task Title:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .MatchWholeWord = True
    End With
 
    Do While rng.Find.Execute
        rng.Select
        Selection.MoveRight Unit:=wdCell
        Selection.Style = ActiveDocument.Styles("BOE Title")
    Loop
End Sub

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting Word to PDF - Bottom of Page is Being Cut Off 2 19
Find fonts in Word 2010 32 63
Word 2016 Installation Problem on Terminal Server 9 58
Paragraph formating? 2 33
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

740 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