PDA

View Full Version : Delete rows from a Table based on style



xoppox
08-06-2010, 02:34 AM
I have a Word document that includes a Table of Content in a Table. I need to run a macro that deletes all rows from the table where Style is different from e.g. Heading 1. Rows with Heading 2/Heading 3 etc. should be deleted. I am not able to produce a macro that does the job. Can anyone guide me here (I am new to VBA)? I am working Word 2003.

gmaxey
08-06-2010, 04:56 AM
Select the table and row this macro:

Sub ScratchMaco()
Dim oTbl As Word.Table
Dim oRow As Word.Row
Set oTbl = Selection.Tables(1)
For Each oRow In oTbl.Rows
If oRow.Cells(1).Range.Style <> "Heading 1" Then oRow.Delete
Next oRow
End Sub


Why aren' you using Word's TOC features to build you TOC?

xoppox
08-06-2010, 05:44 AM
Hi Greg
Thanks for your help. It works. The macro should be run with cursor in first row of Table else I get an error. This is minor issue.
The document attached was just an example. I normally use Word ToC functionalty. This type of ToC is generated by another system that has defined a specific style for each ToC entry. The template actually looks like this (a Table):
-------------------------------------------------------------------
iTOC1 | iTOClink
-------------------------------------------------------------------
In the document ITOC Styles from 1-14 exist. When the system generates the ToC it resolves the the actual ToC entries to different iTOC levels based on the bookmarks in source PDF documents. The iTOClink are resolved to page numbers (this is just explain why I did not use Word ToC functionality)
Thanks again
Kristoffer