Placing a table at the end of a Doc

E

Enrique Rojas

Hello All,

I have been trying to add a table at the end of a document with the
code below. I get Error 4608 Value out of Range. I think it has
something to do with my Set rngParagraphs line. If Anyone can help me
I would appreciate it.

Thanks

Public Sub InsertSignatureBlock(objw As Word.Application)

Dim myTable As Word.Table
Dim myRow As Word.Row
Dim myRange As Word.Range
Dim myCol As Word.Column
Dim rngParagraphs As Range
Set rngParagraphs =
objw.ActiveDocument.Range(objw.ActiveDocument.Content.End)

Set myTable = objw.ActiveDocument.Tables.Add(rngParagraphs, 6, 5)
myTable.Columns(1).Width = objw.InchesToPoints(0.5)
myTable.Columns(2).Width = objw.InchesToPoints(0.5)
myTable.Columns(3).Width = objw.InchesToPoints(0.5)
myTable.Columns(4).Width = objw.InchesToPoints(0.5)
myTable.Columns(5).Width = objw.InchesToPoints(0.5)

With myRow
.Cells(2).Range.InsertAfter ("2")
.Cells(3).Range.InsertAfter ("3")
.Cells(4).Range.InsertAfter ("4")
.Cells(5).Range.InsertAfter ("5")
End With

End Sub
 
L

Lene Fredborg

The error occurs because a table cannot exist as the very last item in a
document. There must be at least an empty paragraph after. If you insert the
table _before_ the last paragraph mark, the error will no longer occur. You
can do this by subtracting 1 from the .Content.End in this code:

Set rngParagraphs = _
objw.ActiveDocument.Range(objw.ActiveDocument.Content.End)

i.e. the last part of the code line will be:
(objw.ActiveDocument.Content.End -1)

Word will automatically add a paragraph mark to the end of the "old" last
paragraph.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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