Can I label rows of a Word table for easy reference in code?

V

victoria.rego

I have a button on my VB form that, when clicked, opens a template
word document with a table, populates the table with values, and
deletes any rows that aren't needed depending on certain checkboxes on
the form and certain cells in an excel document.
For example:

ElseIf Trim(eaExcelApplication.Range("J" & xlsRow)) = "" And
Trim(eaExcelApplication.Range("K" & xlsRow)) = "" Then
With ActiveDocument.Tables(1)
..cell(column:=3, row:=11).Delete wdDeleteCellsEntireRow

This worked fine because I would delete the rows in reverse order so
the row number wouldn't change, however... I've overlooked certain
exceptions that require rows to be deleted. It seems really
inefficient to have an ongoing series of if elseif statements for
every combination of possible scenarios...

I'd like someway of labeling these rows with names (the row headers
for example) so that it won't matter what the row number is, it'll
delete the row with that header.

Is there such a way to do this?

To summarize, right now I tell the application the row number to
delete, but I need to be able to delete these rows no matter what row
number they are. Each row has a row header, so perhaps match the
headers to "section numbers" and write a delete method and pass it the
section number or something?

Thanks,
 
R

Russ

The closest thing to 'named ranges' used in Excel are bookmarks in Word.
They flow with insertions and deletions, as long as you insert using special
techniques. See this webpage and the suggested link at the bottom of this
webpage.
<http://word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm>
So you might want to insert the data in a row, then insert the bookmark into
the row.

A well placed 'place holder' bookmark in each row 'marked for deletion'
could be used. Each bookmark needs an unique name like DeleteMeLater1,
DeleteMeLater2, etc. (or row header names, but no spaces or punctuation
except underscores are allowed in a bookmark name.)

This will delete the whole row that a placeholder bookmark named
DeleteMeLater1 is in.
ActiveDocument.Bookmarks("DeleteMeLater1").Range.Rows(1).Delete

This will delete the whole row that a placeholder bookmark named
DeleteMeLater1 is in and the next two rows.
ActiveDocument.Range(ActiveDocument.Bookmarks("DeleteMeLater1").Range _
.Rows(1).Range.Start, ActiveDocument.Bookmarks("DeleteMeLater1").Range _
.Rows(1).Next.Next.Range.End).Rows.Delete

To create or loop through similar named bookmarks, I would use a counter and
concatenate the first part of name and the current counter value.
ActiveDocument.Bookmarks("DeleteMeLater" & lngCounter).Range.Rows(1).Delete


If you use enclosing bookmarks, you need to be more cautious in inserting
next to them.

The difference between a bookmark and a range is that a bookmark doesn't go
away just because the macros stopped running. You can just use ranges if you
are going to delete the rows before the macros quit and the ranges are
purged from memory. The same cautions apply to inserting before and after
ranges.
 

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