Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

MS Office VBA: insert table

Hello experts,

I have the following table attached.

I was wondering how to set up a Word VBA procedure to insert it to a new blank document.

If you have questions, please contact me.

Regards,
Luis.
TableModel.docx
SOLUTION
Avatar of Louis LIETAER
Louis LIETAER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Sometimes I work on differents PC. I don't have access to .dotx file. I prefer manage this through my Personal Add-in.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To get a VBA macro to do this, you can record one (No VBA knowledge required :-) )

You need the developer showing tab, so right click on the riboon menu and choose Customize the ribbon.

You'll see the word options dialog. in the right hand window, tick the developer tab then ok.

Open a new word doc and save as "mydoc.docm" (or whatever you want to call it).

It must be a .docm which is macro enabled.word doc.

Click the developer tab and choose record macro.

There is a drop down select labelled "Store Macro In" - change this to your just saved docm (rather than normal template).

click ok then work through the steps you want to create a new file, insert a table etc.

When all steps are complete, go back to the original macro enabled word doc and click stop recording on the developer tab.

Resave the Macro enable workbook.

To view the code, choose the Macros button, select your newly record macro and press edit. You will see the code.

It will look something like this:


Sub Macro2()
'
' Macro2 Macro
'
'
    Documents.Add DocumentType:=wdNewBlankDocument
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= _
        9, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.MoveRight Unit:=wdCharacter, Count:=9, Extend:=wdExtend
    Selection.Shading.Texture = wdTextureNone
    Selection.Shading.ForegroundPatternColor = wdColorAutomatic
    Selection.Shading.BackgroundPatternColor = wdColorAutomatic
    Selection.Shading.Texture = wdTextureNone
    Selection.Shading.ForegroundPatternColor = wdColorAutomatic
    Selection.Shading.BackgroundPatternColor = -654246042
    Windows("Doc1.docm").Activate
    Windows("Document2").Activate
    Windows("Doc1.docm").Activate
End Sub

Open in new window


GLHFSS
Hi,
Thank you for all those recommendations.
I am going to take the standards Word approach and save all the changes in my Normal.dotm.
User generated image