?
Solved

MS Word - Finding Text within Table to Change Style

Posted on 2009-05-14
4
Medium Priority
?
342 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

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…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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