VBA or Macro Help: Create new files based on dynamic text in a tab

J

JenInCO

I have 7 tables in a Word doc. Each table has a cell for "Owner". Suzy is
the owner in 4 of the tables, John is the owner in 3. I want to loop through
all possible owners (in this case, 2), find each table that belongs to them,
and create a new file for each owner. At the end of the macro, I'd have a
file called "Suzy.doc" with her 4 tables appended and one called "John.doc"
with his 3 tables appended. I could code this back in the day by looping
through a 'getOwner-readWordDoc-testCondition-writeRecord' loop-until-end.
But, I can't figure out how to do this in a macro or VBA. Anyone have any
ideas? I've got a mock-up Word doc I can send as an example.
 
P

Pesach Shelnitz

Hi,

If the cell containing the owner is located in the same position (row number
and column number) in each table, you could loop through the collection of
tables, check the contents of that cell, and add the table to the applicable
document.
This can be done with code such as the following, which uses the first cell
in each table.

Dim myTable As table
With ActiveDocument
For Each myTable In .Tables
Select Case Left(myTable.Cell(1, 1).Range.Text, _
Len(myTable.Cell(1, 1).Range.Text) - 2)
Case "Suzy"
' Add code to insert myTable into Suzy.doc
Case "John"
' Add code to insert myTable into John.doc
' Add Case statements for other owners.
Case Else
' Add code for default action.
End Select
Next
End With

Write back if this is suitable for your purposes and you need more help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top