reddevil1493
New Member
- Joined
- Dec 13, 2016
- Messages
- 5
Hi,
I have been trying to insert four excel tables into a single page in a word document; to be placed at specific locations. The issue I'm facing is that the code until the selection of summaryVal works fine; but nothing after. In spite of setting the right table from excel when I paste it in word, and try to format it; I do not get an output/ I get the error "Object required". I have defined paragraphs to fit the new table as well. What am I doing wrong?
My code is as follows :
I have been trying to insert four excel tables into a single page in a word document; to be placed at specific locations. The issue I'm facing is that the code until the selection of summaryVal works fine; but nothing after. In spite of setting the right table from excel when I paste it in word, and try to format it; I do not get an output/ I get the error "Object required". I have defined paragraphs to fit the new table as well. What am I doing wrong?
My code is as follows :
Code:
ub ExcelRangeToWord()
Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
Dim WordTable1 As Word.Table
Dim tbl2 As Excel.Range
Dim nRows
Dim nCols
Dim nRows1
Dim nCols1
Dim myDocRange
Dim strval As String
Dim summaryVal As String
'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets("Summary").ListObjects("Table1").Range
'Create an Instance of MS Word
On Error Resume Next
'Is MS Word already opened?
Set WordApp = GetObject(Class:="Word.Application")
'Clear the error between errors
Err.Clear
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application")
'Handle if the Word Application is not found
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
'Make MS Word Visible and Active
WordApp.Visible = True
WordApp.Activate
'Create a New Document
Set myDoc = WordApp.Documents.Add
myDocRange = myDoc.Range
'Copy Excel Table Range
tbl.Copy
'Paste Table into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=True, _
WordFormatting:=False, _
RTF:=False
'Autofit Table so it fits inside Word Document
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
WordTable.Rows.HorizontalPosition = 5
WordTable.Rows.VerticalPosition = 100
nRows = WordTable.Rows.Count
nCols = WordTable.Columns.Count
'format the table
' With WordTable
'.PreferredWidthType = wdPreferredWidthPercent
'.PreferredWidth = 100
'.Columns(1).Width = 20
' .Columns(2).Width = 20
'.Columns(4).Width = 20
'.Columns(5).Width = 20
' End With
For i = 1 To nRows
For j = 1 To nCols
WordTable.Cell(i, j).Range.Font.Name = "Calibri"
WordTable.Cell(i, j).Range.Font.Size = 12
If j = 1 Then
WordTable.Cell(i, j).Range.Shading.BackgroundPatternColor = RGB(176, 196, 222)
ElseIf j = 2 Then
WordTable.Cell(i, j).Range.Shading.BackgroundPatternColor = RGB(216, 191, 216)
ElseIf j = 3 Then
WordTable.Cell(i, j).Range.Shading.BackgroundPatternColor = RGB(238, 232, 170)
ElseIf j = 4 Then
WordTable.Cell(i, j).Range.Shading.BackgroundPatternColor = RGB(222, 184, 135)
ElseIf j = 5 Then
WordTable.Cell(i, j).Range.Shading.BackgroundPatternColor = RGB(143, 188, 143)
End If
Next
Next
'Add the commentary section '
strval = ThisWorkbook.Worksheets("Summary").Range("D2").Value
summaryVal = ThisWorkbook.Worksheets("Description").Range("B2").Value
'WordApp.Selection.TypeText Text:=strval
With WordApp.Selection
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.PageSetup.TopMargin = WordApp.InchesToPoints(0.25)
.Font.Bold = True
.Font.Name = "Arial"
.Font.Size = 32
.Font.Color = RGB(100, 149, 237)
.TypeText (strval)
.TypeParagraph
End With
'Dim ioVal
'ioVal = ThisWorkbook.Worksheets("IO").Range("B3").Value
'WordApp.Visible = True
With WordApp.Selection
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Font.Bold = True
.Font.Name = "Arial"
.Font.Size = 14
.Font.Color = RGB(100, 149, 237)
.TypeText (summaryVal)
.TypeParagraph
End With
WordApp.Visible = True
'With WordApp.Selection
' .ParagraphFormat.Alignment = wdAlignParagraphRight
' .Font.Bold = True
' .Font.Name = "Arial"
' .Font.Size = 14
' .Font.Color = RGB(100, 149, 237)
' .TypeText (ioVal)
' .TypeParagraph
'End With
' Insert second Paragraph'
myDocRange.Collapse Direction:=wdCollapseEnd
With myDocRange
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
End With
Set tbl2 = ThisWorkbook.Worksheets("IO").ListObjects("Table6").Range
tbl2.Copy
myDoc.Paragraphs(2).Range.PasteExcelTable _
LinkedToExcel:=True, _
WordFormatting:=False, _
RTF:=False
'Autofit Table so it fits inside Word Document
Set WordTable1 = myDoc.Tables(2)
WordTable1.AutoFitBehavior (wdAutoFitWindow)
'WordTable1.Rows.HorizontalPosition = 5
'WordTable1.Rows.VerticalPosition = 500
nRows1 = WordTable1.Rows.Count
nCols1 = WordTable1.Columns.Count
WordTable1.Cell(1, 1).Range.Font.Name = "Arial"
WordTable1.Cell(1, 1).Range.Font.Size = 18
WordTable1.Cell(1, 1).Range.Font.Color = RGB(100, 149, 237)
'Dim iProfile
'iProfile = ThisWorkbook.Worksheets(4).Range("B2").Value
'With WordApp.Selection
'WordApp.Selection.Range(strval).Bold = True
'WordApp.Selection.Range(strval).Font.Name = "Arial"
' WordApp.Selection.Range(strval).Font.Size = 32
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
Last edited by a moderator: