Solved

MS Word - Finding Text within Table to Change Style

Posted on 2009-05-14
4
334 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now