We help IT Professionals succeed at work.

MS Word - Table Is Overwritting Previous Data

WonHop
WonHop asked
on
89 Views
Last Modified: 2018-10-10
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

Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED 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
CERTIFIED EXPERT
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.
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED 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
CERTIFIED 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
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

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