Using VBA how do set the position of a table I'm creating.

L

lovetabike

I'm using the following to create a table.

Set tblNew = docActive.Tables.Add( _
Range:=docActive.Range(Start:=0, End:=0), NumRows:=2, _
NumColumns:=3)

How do I change the location of the table? If I put values in Start and End
I get an app error, value out of range. I used 5 for both Start and End.
 
S

StevenM

To: Lovetabike,

I found the following macro to work fine.

Sub testaddtable()
Dim docActive As Document
Set docActive = ActiveDocument

Set tblNew = docActive.Tables.Add( _
Range:=docActive.Range(start:=5, End:=5), NumRows:=2, _
NumColumns:=3)
End Sub

But I ran it on a document which had more than five characters. So I would
assume that you ran it on a document which had less than five characters. If
so, that would be your problem.

The following macro adds five lines and then adds a table.

Sub testaddtableVer2()
Dim docActive As Document
Dim oRange As Range

Set docActive = ActiveDocument
Set oRange = ActiveDocument.Range

oRange.InsertAfter vbCr & vbCr & vbCr & vbCr & vbCr
oRange.Collapse CollpaseEnd

Set tblNew = docActive.Tables.Add( _
Range:=oRange, NumRows:=2, NumColumns:=3)
End Sub


Steven Craig Miller
 
L

lovetabike

Perfect!

StevenM said:
To: Lovetabike,

I found the following macro to work fine.

Sub testaddtable()
Dim docActive As Document
Set docActive = ActiveDocument

Set tblNew = docActive.Tables.Add( _
Range:=docActive.Range(start:=5, End:=5), NumRows:=2, _
NumColumns:=3)
End Sub

But I ran it on a document which had more than five characters. So I would
assume that you ran it on a document which had less than five characters. If
so, that would be your problem.

The following macro adds five lines and then adds a table.

Sub testaddtableVer2()
Dim docActive As Document
Dim oRange As Range

Set docActive = ActiveDocument
Set oRange = ActiveDocument.Range

oRange.InsertAfter vbCr & vbCr & vbCr & vbCr & vbCr
oRange.Collapse CollpaseEnd

Set tblNew = docActive.Tables.Add( _
Range:=oRange, NumRows:=2, NumColumns:=3)
End Sub


Steven Craig Miller
 

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