LauraBlair
New Member
- Joined
- Feb 9, 2021
- Messages
- 13
- Office Version
- 365
- Platform
- Windows
Hi All,
I'm really hoping you may be able to help me. I have a VBA code that copies the tables from 4 tabs in the excel document, and then opens up a new word document and pastes into Word to form an 'Executive Summary'. This all works well, but the formatting of headings and boxes in the word doc that would require data input, is small and would like these cells in the table to merge so the text doesn't look so cluttered. I am struggling to get anything in the code to work in terms of this, I am starting to wonder if copy/pasting in tables is not the best way to do this. Could anyone please help? Thank you
I'm really hoping you may be able to help me. I have a VBA code that copies the tables from 4 tabs in the excel document, and then opens up a new word document and pastes into Word to form an 'Executive Summary'. This all works well, but the formatting of headings and boxes in the word doc that would require data input, is small and would like these cells in the table to merge so the text doesn't look so cluttered. I am struggling to get anything in the code to work in terms of this, I am starting to wonder if copy/pasting in tables is not the best way to do this. Could anyone please help? Thank you
VBA Code:
Sub Executive_Summary()
'
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim myTable As Table
With wdApp
.Visible = True
Set wdDoc = .Documents.Add
' Set up layout
With wdDoc
' Copy the first range
Range("FrontPage").Copy
.Range.Characters.Last.Paste
.Range.InsertAfter Chr(12) ' Insert new line before next table
wdApp.Selection.SplitTable
wdApp.Selection.TypeParagraph
wdApp.Selection.TypeParagraph
' Before second table, insert column and format table
Set myTable = wdDoc.Tables(1)
With myTable
.Columns.Add ' Adds a column to the right
.AutoFitBehavior wdAutoFitWindow 'Autofit table to window
.Range.Font.Size = 25
.Range.Font.Bold = False
End With
' Copy second range
Range("Page2").Copy
.Range.Characters.Last.Paste
.Range.InsertAfter Chr(12) ' Insert new line
' Before third table, insert column and format table
Set myTable = wdDoc.Tables(2)
With myTable
.Columns.Add ' Adds a column to the right
.AutoFitBehavior wdAutoFitWindow 'Autofit table to window
.Range.Font.Size = 9
.Range.Font.Bold = False
End With
' Copy third range
Range("Page3").Copy
.Range.Characters.Last.Paste
.Range.InsertAfter Chr(12) ' Insert new line
' Before forth table, insert column and format table
Set myTable = wdDoc.Tables(3)
With myTable
.Columns.Add ' Adds a column to the right
.AutoFitBehavior wdAutoFitWindow 'Autofit table to window
.Range.Font.Size = 9
.Range.Font.Bold = False
End With
' Copy forth range
Range("Page4").Copy
.Range.Characters.Last.Paste
.Range.InsertAfter Chr(12) ' Insert new line
' Insert column and format table
Set myTable = wdDoc.Tables(4)
With myTable
.Columns.Add ' Add column to the right
.AutoFitBehavior wdAutoFitWindow 'Autofit table to window
.Range.Font.Size = 9
.Range.Font.Bold = False
End With
End With
End With
End Sub