VBA - Excel to Outlook - Center pasted table

oddzac

New Member
Joined
Aug 12, 2022
Messages
25
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hey all, I'm *really* fresh to VBA as a whole and have a *very* general concept of how to read the scripting

I've frankensteined together some code that mostly does what I need, but I'm having a formatting issue with one section of my code.
I'm sure it's a single line that I'm just not plugging in right, but I've tried a handful of different methods that I haven't been able to implement..

Here's the section that does everything I need except the center alignment:

VBA Code:
'Copy contents

    Sheets("Tables").Select
    Range("J6:R145").Select
    Range("J6").Activate
    Selection.Copy

'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
   
'Paste as image
   
    wordDoc.Range.PasteAndFormat Type:=wdChartPicture
    wordDoc.Range.Select
    Selection.Rows.Alignment = wdAlignRowCenter

Current Result:
1676666266624.png



I'm betting it's a matter of bad syntax but here's what I've plugged in so far:
Selection.PageSetup.CenterHorizontally = True
Selection.HorizontalAlignment = xlCenter
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
and the above-listed Rows.Alignment


Any help would be appreciated!


PS: Bonus points if you can teach how to keep it from deleting the email signature when it pastes
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
maybe
wordDoc.Application.Selection.Paragraphs.Alignment = 1
however, it might be better to be more specific with the object variable:
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor
 
Upvote 0
Solution
Thank you!
I also had some luck in a similar thread and wound up with this:

VBA Code:
'Paste as image
    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
 
Upvote 0

Forum statistics

Threads
1,223,640
Messages
6,173,502
Members
452,517
Latest member
SoerenB

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