将表格向右移动以与Outlook正文中的文本对齐



我正试图使用Excel Macro从outlook发送一封电子邮件,我需要一个解决方案来解决如何将我复制粘贴的表从Excel移动到outlook正文的问题,该正文将始终对齐到左侧,我希望将其移到右侧,以便与顶部的其他内容完全匹配/对齐。

我的代码

Sub Table_CopyPaste()
Dim outlook As Object 
Dim newEmail As Object 
Dim xInspect As Object 
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application") Set newEmail = outlook.CreateItem(0)
StrBody1 = "<o:p>&nbsp;</o:p><p class=MsoNormal><span lang=EN-US style='color:#1F497D'><span style='font:11.0pt 'Calibri'>Hello,</Span></p><p class=MsoNormal><span style='font:11.0pt 'Calibri(Body)'>Attached you can find a file with all your Status for month 10_2019. </p></span><p class=MsoNormal><span style='font:11.0pt 'Calibri(Body)'>Below you can find an overview of your current status and your unit status.</span></span></p>" _ 
& "<p class=MsoListParagraph style='margin-left:53.4pt;text-indent:-18.0pt;mso-list:l0 level1 lfo2'><span style='font:11.0pt 'Calibri'><span lang=EN-US style='color:#1F497D'>1) &nbsp;&nbsp;&nbsp;&nbsp; We have extended the report with additional information, so you can develop a more complete view on your status:" _ 
& "<br>" _ 
& "<br>&nbsp;&nbsp;&nbsp;&nbsp;- &nbsp;&nbsp;&nbsp;&nbsp HR" _ 
& "<br>&nbsp;&nbsp;&nbsp;&nbsp;- &nbsp;&nbsp;&nbsp;&nbsp Accounts" _ 
& "<br>&nbsp;&nbsp;&nbsp;&nbsp;- &nbsp;&nbsp;&nbsp;&nbsp Finance" _

With newEmail
.To = "Test@mail.com"
.CC = ""
.BCC = ""
.Subject = "Data"
.HTMLBody = StrBody1
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheets("Statistics_Sheet").Range("A3:D6").Copy
pageEditor.Application.Selection.Start = Len(.HTMLBody)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.Display
'.Send
Set pageEditor = Nothing
Set xInspect = Nothing End With
Set newEmail = Nothing Set outlook = Nothing
End Sub

这是我第一次将前景与宏观相结合,所以不知道如何解决这个问题。代码运行良好,一旦我们运行此代码,我需要将表与要点"财务"对齐

在到处搜索了太多之后,我发现不可能移动它,但这里的一个解决方法是在该范围内添加一些额外的空白单元格,这样当它复制粘贴到outlook时,你就会使它与文本对齐(调整单元格宽度以更改对齐方式(

但我发现了一个像符咒一样工作的代码,

Sub PublishTable()
Dim WB As ThisWorkbook, P As String, WS As Worksheet, Rng As Range, New_WB As Workbook, RNG2 As Range, FolderPath As String
Set WB = ThisWorkbook
Set WS = WB.Sheets("Statistics_Sheet")
FolderPath = Application.ActiveWorkbook.Path
Set Rng = Sheets("Statistics_Sheet").Range("C3:F6")
P = FolderPath & "Calculation_of_exception_status.html"
Workbooks.Add
Set New_WB = ActiveWorkbook
ThisWorkbook.Activate
Rng.Copy
New_WB.Activate
ActiveCell.PasteSpecial xlPasteValues
ActiveCell.PasteSpecial xlPasteFormats
ActiveCell.PasteSpecial xlPasteColumnWidths
ActiveCell.PasteSpecial xlPasteFormats
New_WB.PublishObjects.Add(xlSourceRange, P, New_WB.Sheets(1).Name, New_WB.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True)
ActiveWorkbook.Close SaveChanges:=False
Dim fso As New FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim Final_File As Scripting.TextStream
Set Final_File = fso.OpenTextFile(P, ForReading)
StrTable2 = Final_File.ReadAll
End Sub

当您在outlook正文中使用Strtable2时,请使用以下代码,根据您的要求调整"20.3pt"。

olMailItm.HTMLBody = "<table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 style='margin-left:20.3pt;border-collapse:collapse'>" & StrTable2 & "</Table>"

如果它插入的是一个实际的HTML<table>元素,则可以向.HTMLBody添加样式表

在顶部,Dim是一个变量:

Dim sStyles As String
sStyles = "<style> table {margin-left:150px;} table table {margin-left:0px;} </style><br>"

然后在你设置.HTMLBody的地方,像这样添加它:

.HTMLBody = sStyles & StrBody1

然后将代码中的150数字调整为您想要的任何数字,使其与您想要的位置对齐。

最新更新