PDA

View Full Version : How do add text before and after inserting a table?



jason_kelly
03-23-2017, 10:59 AM
Hello,

How would I go about adding some text before and after inserting a table?

Example would be.

Text Here #1
<WORD TABLE>
Text Here #2

As of right now, my code sticks the text side-by-side and after the table.


var wordApp = new ActiveXObject("Word.Application")

var doc = wordApp.Documents.Add()

doc.Range().Insertbefore("Text Here1")

doc.PageSetup.LeftMargin = CentimetersToPoints(0.75)
doc.PageSetup.RightMargin = CentimetersToPoints(0.75)

doc.Tables.Add(Range=doc.Range(0, 0), numrows=1, numcolumns=10)
doc.Tables(1).Range.Font.Size = 9
doc.Tables(1).Range.Font.Name = 'Arial'
doc.Tables(1).Rows(1).Range.Font.Bold = true

doc.Tables(1).Cell(1,1).Range.Text = '#'
doc.Tables(1).Cell(1,2).Range.Text = 'File Number'
doc.Tables(1).Cell(1,3).Range.Text = 'Subject'
doc.Tables(1).Cell(1,4).Range.Text = 'Branch'
doc.Tables(1).Cell(1,5).Range.Text = 'Division'
doc.Tables(1).Cell(1,6).Range.Text = 'Related Documents'
doc.Tables(1).Cell(1,7).Range.Text = 'Current Status'
doc.Tables(1).Cell(1,8).Range.Text = 'Assigned To'
doc.Tables(1).Cell(1,9).Range.Text = 'Action Required'
doc.Tables(1).Cell(1,10).Range.Text = 'Last Updated'

doc.Tables(1).AutoFitBehavior(1)

doc.Range().Insertafter("Text Here2")

wordApp.Visible = true

wordApp.Activate()

wordApp.WindowState = 1

gmaxey
03-23-2017, 03:17 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = Selection.Range
With oRng
.InsertBefore "***" & vbCr
.Collapse wdCollapseEnd
.InsertAfter "YYY"
.Collapse wdCollapseStart
.Tables.Add oRng, 2, 5
End With
lbl_Exit:
Exit Sub

End Sub