PDA

View Full Version : VBA coding for Word table to combine macros and left align first column only



Maza
10-10-2019, 05:41 AM
Looking for some help please with this coding below, they work seperately, but cannot workout how to piece them together to be incorporated into one button. This is to paste an different excel tables into a word template table placeholder. Thanks

Sub CGPasteTable()
Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, Placement:= _
wdInLine, DisplayAsIcon:=False
End Sub

With Selection
.Tables(1).Select
.Style = ActiveDocument.Styles("CG Table Text")
.Tables(1).AutoFitBehavior (wdAutoFitContent)
.Tables(1).AutoFitBehavior (wdAutoFitWindow)
.ParagraphFormat.Alignment = wdAlignParagraphRight
With .Cells
.VerticalAlignment = wdCellAlignVerticalCenter
.Height = CentimetersToPoints(0.44)
End With
End With
End Sub

Sub CGleftalignfirstcolumn()
With Selection
.SelectColumn
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
End Sub

gmayor
10-10-2019, 05:55 AM
Assuming the style exists

Sub CGPasteTable()
Dim oTable As Table
Dim oCell As Cell
Selection.PasteSpecial _
link:=False, _
DataType:=wdPasteRTF, _
Placement:=wdInLine, _
DisplayAsIcon:=False
Set oTable = Selection.Tables(1)
With oTable
'.Style = ActiveDocument.Styles("CG Table Text")
.AutoFitBehavior wdAutoFitWindow
.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
With .Range.Cells
.VerticalAlignment = wdCellAlignVerticalCenter
.Height = CentimetersToPoints(0.44)
End With
For Each oCell In .Columns(1).Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Next oCell
End With
Set oTable = Nothing
Set oCell = Nothing
End Sub

Maza
10-10-2019, 07:32 AM
Thanks for your reply, much appreciated.

Unfortunately its not working for me. I have a table place holding that i pull out of quick parts, this has a figure title row at the top and source row beneath and one merged row where i want to paste my table. the macro you provided pastes in, but doesnt then do any of the other commands i list. the style names i have double checked and they are correct. Any ideas?

gmayor
10-11-2019, 04:42 AM
Without the document it is difficult to understand what is going on. From your comment it appears the table is not being correctly identified.
Can you post sample documents with and without the pasted table.