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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
GLHFSS
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
GLHFSS
ASKER