Centering Excel Table pasted into Word with VBA

alx1056

New Member
Joined
Jul 11, 2018
Messages
8
Hello,

I am new to VBA and I am trying to paste a table to Word from Excel using strictly VBA.

My only issue is that I cannot center the table. I also can't get it to backspace once to the table to make it look nicer.

Here is my code:

Code:
Option Explicit
Sub talkToWord()

    Dim wdApp As Word.Application
    Dim myCat As Integer
    myCat = InputBox("Enter your Category: ")
    
    Set wdApp = New Word.Application
    With wdApp
        .Visible = True
        .Activate
        .Documents.Add
        
        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .BoldRun
            .Font.Underline = True
            .TypeText "FY 19 CAT " & myCat
            .BoldRun
            .Font.Underline = False
            .TypeParagraph
            .Font.Size = 11
            .TypeParagraph
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .BoldRun
            .TypeText ("Grade Number:")
            .TypeParagraph
            .TypeText ("Config #:")
            .TypeParagraph
            .TypeText ("Grade Name:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "Dept:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "Class/Subclass:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "Season Code:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "TimeFrame:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "Grade Type:")
            .TypeParagraph
            .TypeText (Chr(9) & "-" & "Index Breakpoint Bands by Volume Grade:")
            .TypeParagraph
            .BoldRun
            .TypeParagraph
            .InsertParagraph
        End With
        .Selection.WholeStory
        .Selection.ParagraphFormat.LeftIndent = InchesToPoints(-0.7)
        .Selection.ParagraphFormat.SpaceAfter = 5
        .ActiveDocument.PageSetup.TopMargin = InchesToPoints(0.3)
        .Selection.EndKey Unit:=wdStory
        .Selection.InsertBreak Type:=6
        .Selection.TypeBackspace
        Worksheets("Sheet2").Range("A1", "B4").Copy
        wdApp.Selection.Paste
        wdApp.Selection.TypeBackspace
        Selection.ParagraphFormat.Alignment = wdAlignRowCenter
        
    End With
    
            
   
End Sub

Thank you!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try:
Code:
Sub TalkToWord()
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.Range
Dim myCat As Long: myCat = InputBox("Enter your Category: ")
    
With wdApp
  .Visible = True
  Set wdDoc = .Documents.Add
  With wdDoc.Range
    .PageSetup.TopMargin = InchesToPoints(0.3)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Bold = True
    .Font.Underline = True
    .InsertAfter "FY 19 CAT " & myCat
    .InsertParagraphAfter
    Set wdRng = .Characters.Last
    With wdRng
      .ParagraphFormat.Alignment = wdAlignParagraphLeft
      .ParagraphFormat.LeftIndent = InchesToPoints(-0.7)
      .ParagraphFormat.SpaceAfter = 5
      .Font.Underline = False
      .Font.Size = 11
      .InsertParagraphAfter
      .InsertAfter ("Grade Number:")
      .InsertParagraphAfter
      .InsertAfter ("Config #:")
      .InsertParagraphAfter
      .InsertAfter ("Grade Name:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Dept:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Class/Subclass:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Season Code:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "TimeFrame:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Grade Type:")
      .InsertParagraphAfter
      .InsertAfter (Chr(9) & "-" & "Index Breakpoint Bands by Volume Grade:")
      .InsertParagraphAfter
      .InsertParagraphAfter
      .Collapse wdCollapseEnd
      .InsertParagraphAfter
      .InsertBreak Type:=wdLineBreak
      .Collapse wdCollapseEnd
      Worksheets("Sheet2").Range("A1", "B4").Copy
      .Paste
      .Tables(1).Rows.Alignment = wdAlignRowCenter
    End With
  End With
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,636
Messages
6,173,484
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top