Search code examples
vbams-wordoffice-2007

Convert Text to Table VBA MS Word - AutoFit to Contents not working


I am trying to automate conversion of text to a table. When I selected "Auto-fit to contents" while recording a macro, I got the following code:

    WordBasic.TextToTable ConvertFrom:=1, NumColumns:=2, NumRows:=6, _
    InitialColWidth:=wdAutoPosition, Format:=0, Apply:=1184, AutoFit:=1, _
    SetDefault:=0, Word8:=0, Style:="Table Grid"

However, when I run this code as part of the unchanged recorded macro, it does not auto-fit to contents. Anyone have any ideas where I went wrong?

I want it to autofit based on content, but the macro recorder won't record that part. I'm in ms word 2007. I've googled for 30 minutes but to no avail on what to do.


Solution

  • To auto-fit to the contents, I added the last line below:

    WordBasic.TextToTable ConvertFrom:=1, NumColumns:=2, NumRows:=6, _
    InitialColWidth:=wdAutoPosition, Format:=0, Apply:=1184, AutoFit:=1, _
    SetDefault:=0, Word8:=0, Style:="Table Grid"
    Selection.Tables(1).AutoFitBehavior wdAutoFitContent     ' *** Added this