Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking for text in Word table (2003)

    I want to use a Word table as a to-do list, with the following feature: The table will have four columns: TASK; WHO; DUE DATE; DONE. Users will insert an X in the DONE column when a task is completed. No big deal so far. I've discovered the folks I work with don't pay a lot of attention to whether there is an X in the DONE column, so I want the entire row that contains a completed task to have some sort of special formatting (strike-through; highlighted in grey; whatever). So, what I need to do is write a little macro that either (1) automatically applies the special formatting to the text in a row whenever someone puts an X in the DONE column, or (2) at least can periodically run through the whole table and check to see if there's an X in the DONE column for each row, and apply the correct formatting to the text in that row if there is an X.

    Thanks for any help, pointers, etc.

    There's probably a lot about this process that I don't know how to code, but I think I can figure most of it out EXCEPT for the part about the X triggering the formatting. That is, can anyone clue me in regarding code that either monitors keystrokes to look for an X being typed in the DONE column (alternative (1), above), or marches through the table looking at each cell in the DONE? column, and in either case then triggers the formatting I want?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Checking for text in Word table (2003)

    Here is a macro that will highlight all rows containing an x or X in the fourth column in the first table in the document:

    <code>Sub HighlightX()</code>
    <code> Dim oCell As Cell</code>
    <code> ActiveDocument.Tables(1).Columns(4).Select</code>
    <code> For Each oCell In Selection.Cells</code>
    <code> If InStr(UCase(oCell.Range.Text), "X") > 0 Then</code>
    <code> oCell.Row.Shading.BackgroundPatternColorIndex = wdGray25</code>
    <code> End If</code>
    <code> Next oCell</code>
    <code> Selection.HomeKey Unit:=wdStory</code>
    <code> Set oCell = Nothing</code>
    <code>End Sub</code>

    Monitoring keystrokes might be possible, but it would probably slow down Word.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    Have you considered using a Form? Then you could run a macro when a check box was checked that would apply the strikethrough formatting.

  4. #4
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    This works great. I don't think I understand, however, the line:

    oCell.Row.Shading.BackgroundPatternColorIndex = wdGray2

    Specifically, what would this look like if I used an object, rather than a variable, for "oCell" at the beginning of this statement? In that statement, is oCell the equivalent of "ActiveDocument..Tables(1).Cell(X,Y), where X and Y are the coordinates of the cell?

    ActiveDocument.Tables(1).Cell(x,y)

  5. #5
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    Yes, thanks. I also thought of using a MacroButton field in the DONE column. The problem with both of those approaches is that other users cannot easily expand the list to add more items. They'd either have to know how to modify forms, or how to insert MacroButton fields. They all know enough to insert a new row on a table, but that's about as far as they can go.

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    Hi Michael,

    You might have something like the following macro run every time the document is saved (by intercepting the FileSave command), and maybe also add a toolbar button to run it on demand. Someone else on the board may know how to schedule it to run periodically, but offhand I don't know (though come to think of it, that's a neat idea -- sort of like a Unix cron tab for your macros; but I digress).

    This assumes the table you described in your initial post. Any text (x or otherwise) placed in the "Done" column for a particular row will cause the text to strikethrough. Additionally, if the text is removed from the column (i.e., the task is 'undone'), the strikethrough is removed.

    <pre>Sub MarkCompleteTasks()
    Dim tbl As Table
    Dim r As Row
    Dim i As Integer
    Set tbl = ActiveDocument.Tables(1)

    For i = 2 To tbl.Rows.Count
    Set r = tbl.Rows(i)
    If r.Cells(4).Range.Characters.Count > 1 Then
    r.Cells(1).Range.Font.StrikeThrough = True
    r.Cells(2).Range.Font.StrikeThrough = True
    r.Cells(3).Range.Font.StrikeThrough = True
    Else
    r.Range.Font.StrikeThrough = False
    End If
    Next i
    End Sub
    </pre>


    There are certainly ways to improve efficiency, but I was shooting for readability.

    HTH

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Checking for text in Word table (2003)

    oCell is indeed the equivalent of ActiveDocument.Tables(1).Cell(X,Y). Working with cell coordinates is notoriously slow, so I avoided using them - see Maximising the performance of Word tables on the Word MVP site.

  8. #8
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    This is great. Thanks. I think I need to learn more about using ranges. (I tend to think in terms of the current selection.) Any suggestions regarding a good text, or online tutorial re ranges, range objects, etc?

  9. #9
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for text in Word table (2003)

    The article on the MVP site you note is very helpful. It'll take me awhile to digest. I'm sure with that material I can figure out the answer to the following question, but if you've got the time and the inclination....: Let's say I want to (a) insert a new row after the LAST row in a table, and then ([img]/forums/images/smilies/cool.gif[/img] insert specific text in LAST cell in the row I've just inserted. I guess I could figure out how to do this if I could figure out how to make the macro put the cursor in the last row, and then just use "insert row below", followed by a bunch of operations on the current selection, but there's got to be a more efficient way to do this using ranges, etc.. Any help on finding the last row, inserting text into the last cell in the new row, etc. would be greatly appreciated, but I realize that at some point you've got better things to do than help me avoid figuring out how to do that myself. Thx.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Checking for text in Word table (2003)

    That's not so hard:

    With ActiveDocument.Tables(1)
    .Rows.Add
    .Cell(.Rows.Count, .Columns.Count).Range.Text = "Hello World"
    End With

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •