I'm running Excel VBA code to copy/paste/format emails to simplify reports.
The macro runs through several separate ranges in a worksheet. For each range it copies the selected area, pastes it into the email and centers the pasted table.
This is an expansion on my previous question: How Do I Center a Pasted Table with VBA
Even with .Range.InsertParagraphBefore
to create a new line, my tables are nesting inside of each other as they load.
Dims for Context:
Sub Macro7()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim rng As Range
Dim OutApp As Object
Dim outMail As Object
Dim Location As String
Dim Signature As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Open new mail item
Dim outlookApp As Object
Set outlookApp = CreateObject("Outlook.Application")
Set outMail = outlookApp.CreateItem(0)
'Get Word editor
outMail.Display
Dim wordDoc As Object
Set wordDoc = outMail.GetInspector.WordEditor
Problem Code:
'Copy contents
Sheets("Tables").Select
Range("AB7:AI75").Select
Range("AB7").Activate
Selection.Copy
'Paste as image (Centered)
wordDoc.Range.InsertParagraphBefore 'Create new empty paragraph before signature
wordDoc.Paragraphs.first.Range.PasteAndFormat Type:=wdChartPicture
wordDoc.Range.InsertParagraphBefore
With wordDoc.Tables(1).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
'======== SECOND TABLE ========
'Copy contents (2)
Sheets("Tables").Select
Range("P7:Z29").Select
Range("P7").Activate
Selection.Copy
'Paste as image (Centered)(2)
wordDoc.Range.InsertParagraphBefore
wordDoc.Range.InsertParagraphBefore 'Create new empty paragraph before signature
wordDoc.Paragraphs.first.Range.PasteAndFormat Type:=wdChartPicture
With wordDoc.Tables(1).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
'======== THIRD TABLE ==========
'Copy contents (3)
Sheets("Tables").Select
Range("F7:M30").Select
Range("F7").Activate
Selection.Copy
'Paste as image (Centered)(3)
wordDoc.Range.InsertParagraphBefore 'Create new empty paragraph before signature
wordDoc.Paragraphs.first.Range.PasteAndFormat Type:=wdChartPicture
With wordDoc.Tables(1).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
Each pasted table is dropped inside of the top line of the table before, nesting one inside the other, and I'm looking for a way to break them apart.
This pastes the tables sequentially one after the other (not before the other), the idea os you look for the last paragraph and use the .previous property to insert the table in the paragraph before the last paragraph that it has been created on the lines before.
Sub Macro7()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim Rng As Range
Dim OutApp As Object
Dim outMail As Object
Dim Location As String
Dim Signature As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Open new mail item
Dim outlookApp As Object
Set outlookApp = CreateObject("Outlook.Application")
Set outMail = outlookApp.CreateItem(0)
'Get Word editor
outMail.Display
Dim wordDoc As Object
Set wordDoc = outMail.GetInspector.WordEditor
'Copy contents
Sheets("Tables").Select
Range("AB7:AI75").Select
Range("AB7").Activate
Selection.Copy
'Paste as image (Centered)
Dim insertPoint As Object
wordDoc.Paragraphs.first.Range.InsertParagraphBefore 'Create new empty paragraph before signature
Set insertPoint = wordDoc.Paragraphs.first
insertPoint.Range.InsertParagraphBefore 'Create another
insertPoint.Previous.Range.PasteAndFormat Type:=wdChartPicture
With wordDoc.Tables(1).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
'======== SECOND TABLE ========
'Copy contents (2)
Sheets("Tables").Select
Range("P7:Z29").Select
Range("P7").Activate
Selection.Copy
'Paste as image (Centered)(2)
insertPoint.Range.InsertParagraphBefore 'Create new empty paragraph before signature
insertPoint.Range.InsertParagraphBefore 'Create another
insertPoint.Previous.Range.PasteAndFormat Type:=wdChartPicture
With wordDoc.Tables(2).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
'======== THIRD TABLE ==========
'Copy contents (3)
Sheets("Tables").Select
Range("F7:M30").Select
Range("F7").Activate
Selection.Copy
'Paste as image (Centered)(3)
insertPoint.Range.InsertParagraphBefore 'Create new empty paragraph before signature
insertPoint.Range.InsertParagraphBefore 'Create another
insertPoint.Previous.Range.PasteAndFormat Type:=wdChartPicture
With wordDoc.Tables(3).Rows
.WrapAroundText = 0 'If this is true does not work
.Alignment = 1
End With
End Sub
``