Avatar of WonHop
WonHop
Flag for United States of America asked on

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#)
VBAMicrosoft Word

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Subodh Tiwari (Neeraj)

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

GrahamSkan

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.
Subodh Tiwari (Neeraj)

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. :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
WonHop

ASKER
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)

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. ;)
WonHop

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

WonHop
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Subodh Tiwari (Neeraj)

You're welcome! Glad we could help.