We help IT Professionals succeed at work.

MS Office VBA: insert table

42 Views
Last Modified: 2020-04-14
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
Comment
Watch Question

Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Luis DiazIT consultant

Author

Commented:
Sometimes I work on differents PC. I don't have access to .dotx file. I prefer manage this through my Personal Add-in.
Production Manager
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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
Luis DiazIT consultant

Author

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.