Avatar of Luis Diaz
Luis Diaz 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
Microsoft OfficeVBAMicrosoft Word

Avatar of undefined
Last Comment
Luis Diaz

8/22/2022 - Mon
SOLUTION
Louis LIETAER

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Luis Diaz

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
John Korchok

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
therealmongoose

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
ASKER
Luis Diaz

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.
Save-table-template.png
Your help has saved me hundreds of hours of internet surfing.
fblack61