PDA

View Full Version : How to add text AFTER a table using a macro



clloyd1226
03-10-2008, 11:20 AM
I am trying to add a paragraph after a MSWord table and am unable to exit the table (the text gets added in the table). I am a VBA newbie so it could be something obvious.

Dim oRng As Word.Range
Dim oPara As Word.Paragraph
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
2
ChangeFileOpenDirectory _
"C:\Documents and Settings\chris lloyd\My Documents\My Pictures\"
Selection.InlineShapes.AddPicture FileName:= _
"C:\Documents and Settings\XXXXXXXX\My Documents\My Pictures\XXXXXX.JPG" _
, LinkToFile:=False, SaveWithDocument:=True
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="XXXXXXX"
Selection.TypeText Text:="XXXXXXXX"


?????? What code goes here to start a new paragraph after the table?

Tinbendr
03-10-2008, 01:50 PM
Gee, I couldn't figure out how to do it with Selection! :think:

Here's how-to with Range, though...

Sub MySub()
Dim oRng As Word.Range
Dim oPara As Word.Paragraph
Set oRng = ActiveDocument.Range
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=2
oRng.EndOf wdStory, wdMove
oRng.InsertAfter "XXXXXXX" & vbCr
oRng.InsertAfter "XXXXXXXX"
End Sub

clloyd1226
03-10-2008, 01:55 PM
:friends:

fumei
03-12-2008, 12:04 PM
1. why are you using ChangeFileOpenDirectory?
2. It is not possible to insert a table without there being a paragraph after. Even if you go right to the end of a document, and insert a table...there will be a paragraph after it.

Dim oTable As Table
Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=1, NumColumns:=2)
With oTable
.Cell(1, 1).Range.InlineShapes _
.AddPicture FileName:="c:\test\spoon.gif"
.Cell(1, 2).Range.Text = "XXXXXXXXXX"
End With
Set oTable = Nothing
Selection.MoveDown Unit:=wdLine, Count:=1
will:

- make a 1 row, 2 column table at the Selection
- insert the given image into Cell 1, 1
- insert the given text into Cell 1, 2
- move the Selection to the paragraph following the table.