Consulting

Results 1 to 4 of 4

Thread: VBA coding for Word table to combine macros and left align first column only

  1. #1
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    2
    Location

    VBA coding for Word table to combine macros and left align first column only

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    2
    Location
    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?
    Last edited by Maza; 10-10-2019 at 07:56 AM.

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •