MS Word - Table Is Overwritting Previous Data

WonHop
WonHop used Ask the Experts™
on
The code below adds Text and creates a Table.  The code add the text "Appendix A" & "Total items: 10"
The problem is that it will add the text.  When the table is created, it overwrites the text.
How can I add the table under/after the text?
I thought "ActiveDocument.Content.InsertParagraphAfter" would fix it.

strAppendixAName = "Appendix_A.docx"
strAppendixAPath = CurrentProject.Path
strAppendixAFullPath = strAppendixAPath & "\" & "Appendix_A.docx"

inttblAppendixAWorking =10

Dim objWord
Dim objDoc
Dim objSelection

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection

objWord.Visible = True
objDoc.SaveAs (strAppendixAFullPath)

 objDoc.Select
 objDoc.Activate
 objDoc.ActiveWindow.Activate

objSelection.TypeText ("Appendix A" & vbCrLf & "Total items:  " & inttblAppendixAWorking & vbCrLf & vbCrLf)

ActiveDocument.Content.InsertParagraphAfter

intRows = inttblAppendixAWorking
intCols = 2

 'Create Table
 Set tblAppA = objDoc.Tables.Add(Range:=objDoc.Range, _
                                                    NumRows:=intRows, _
                                                    NumColumns:=intCols)
tblAppA.Range.Font.Size = 12
tblAppA.Borders.Enable = True

tblAppA.Columns.PreferredWidthType = wdPreferredWidthPoints
tblAppA.Columns(1).PreferredWidth = CentimetersToPoints(3)
tblAppA.Columns(2).PreferredWidth = CentimetersToPoints(14#)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
Your code will replace the whole of the document range with the table.

I can't test anything at the moment, but try this:
Set tblAppA = objDoc.Tables.Add(Range:=objDoc.Bookmarks("\EndOfDoc"),Range, _
                                                    NumRows:=intRows, _
                                                    NumColumns:=intCols)

Open in new window

It should add the table at the end of the document. If this is not what is needed you will have to keep track of range of the text that was entered.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
I guess Graham's code has a typo, it should be...

Set tblAppA = objDoc.Tables.Add(Range:=objDoc.Bookmarks("\EndOfDoc").Range, _
                                                    NumRows:=intRows, _
                                                    NumColumns:=intCols)

You can also try...

Set tblAppA = objDoc.Tables.Add(Range:=objWord.Selection.Range, _
                               NumRows:=intRows, _
                               NumColumns:=intCols)

Open in new window

GrahamSkanRetired
Top Expert 2012

Commented:
Thanks, Neeraj.

You are, of course, correct. I typed a comma where a full stop should have been. Still understandable in text, but  fatal in a macro.

I am not currently in a position to test any code.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
No problem Graham. You mentioned in your reply that you were not able to test the code at that moment and that's why I pointed that out. :)

Author

Commented:
Thank you guys.  That worked.  Is there a way that I can award extra points to give both of you Full Credit points?
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Is there a way that I can award extra points to give both of you Full Credit points?
You can accept the Graham's answer as the Accepted Solution since he answered it first.
BTW you can award full bonus points to all the experts who you think answered your question to produce the desired output and you can also mark multiple answers as helpful. ;)

Author

Commented:
Thank you both for you help.  I hope I awarded the points correctly.

WonHop
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome! Glad we could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial