MS Word - Table Is Overwritting Previous Data

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#)
LVL 2
WonHopAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
GrahamSkanRetiredCommented:
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.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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. :)
0
WonHopAuthor Commented:
Thank you guys.  That worked.  Is there a way that I can award extra points to give both of you Full Credit points?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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. ;)
0
WonHopAuthor Commented:
Thank you both for you help.  I hope I awarded the points correctly.

WonHop
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome! Glad we could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.