PDA

View Full Version : Convert Text to Table



bilbo85
02-15-2018, 11:11 AM
Hi,

I have a macro in Word which inserts some text to the end of the document. I would then like to convert the new text into a table and format it. The problem I am having is that it keeps converting the whole document into a table.

How would I only convert the new text into a table?

This is what converts the whole document which is no good:

ActiveDocument.Range.ConvertToTable Separator:=wdSeparatebyTabs

Thanks!

gmaxey
02-15-2018, 11:46 AM
VBA does what you tell it to. Nothing else. You are telling it to convert the activedocument range (all of it) to a table. You don't show what inserts the text, but look at this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 2/15/2018
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
oRng.InsertBefore vbCr
oRng.Text = "Some inserted text" & vbTab & "Some more text"
oRng.ConvertToTable Separator:=wdSeparateByTabs
lbl_Exit:
Exit Sub
End Sub