Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have movie scripts. First column is time code, second column is speaker, third column is dialog.

I want to loop through the entire script, but only doing something to the third column (dialog).

I already have a macro for what I do to the third column. I just need it to:

  • start in row 1 column 1
  • tab to row 1 column 2
  • tab again to row 1 column 3 (then do my pre-built macro)
  • then tab again taking me back to r2 c1

But it has to stop when it hits an empty table cell. My macros keep hitting infinite loops, continuing to create new cells as the tab key is programmatically checked despite many different checks for end of document. All solutions out there assume it is an ordinary, non-tabled document, and code such as:

Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")
    '(Do something)
Loop

fails.

share|improve this question
    
Sub looper() ' an example of my failed vba: With ActiveDocument.Content.Find Do While .Execute(Forward:=True, Format:=True) = True Selection.MoveLeft Unit:=wdCell Selection.MoveLeft Unit:=wdCell Selection.MoveLeft Unit:=wdCell Application.Run MacroName:="FixParagraph2" Loop End With End Sub –  chris G Mar 29 '13 at 22:26
    
Hi Chris! Welcome to Super User. Please edit your post to include additional info. It'd be great if you could check our formatting help and use proper capitalization. Thank you! –  slhck Mar 29 '13 at 22:38

1 Answer 1

You can iterate through rows and cells of a table programmatically without tabbing to each cell:

Sub ProcessScriptTable()
  Dim oTbl As Table
  Set oTbl = ActiveDocument.Tables(1)

  For Each oRow In oTbl.Rows
    Set oCell = oRow.Cells(3)

    'Do something with each cell
    MsgBox oCell.Range.Text
  Next

End Sub
share|improve this answer
    
I tried @Adam's solution, but it just shows a message box each time it accesses a cell in column 3 without actually performing the steps in my pre-built macro. It doesn't do anything to the cell. Here is the macro as it stands: Sub Descriptive_3() Dim oTbl As Table Set oTbl = ActiveDocument.Tables(1) For Each oRow In oTbl.Rows Set oCell = oRow.Cells(3) 'prebuilt macro that works as a standalone macro trimming sentences as I need. run Application.Run MacroName:="FixParagraph" MsgBox oCell.Range.Text Next End Sub –  chris G Apr 1 '13 at 20:29
    
@Chris, are you having trouble integrating the existing macro with the above code? If so, please edit your post with your macro and we can help. –  Adam Apr 4 '13 at 23:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.