首页 > 解决方案 > 向右移动表格以与 Outlook 正文中的文本对齐

问题描述

我正在尝试使用 Excel 宏从 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 与宏集成,所以不知道如何解决这个问题。一旦我们运行此代码,代码就可以正常工作我需要将表格与项目符号“财务”对齐

标签: htmlvbaoutlook

解决方案


经过在这里和那里进行太多搜索后,我发现它无法移动它,但这里的解决方法是在该范围内添加一些额外的空白单元格,以便当它复制粘贴到 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>"

推荐阅读