更改 Run de Bruin Outlook Mail 的列宽

问题描述 投票:0回答:1

我正在使用 Ron de Bruins 代码通过电子邮件发送范围,但我还没有弄清楚如何更改发送的 html 范围的列宽。我尝试添加 EntireRow.Autofit 和 EntireColumn.Autofit ,但我希望某些列的宽度精确为 15,但不要太多。 感谢您的时间和帮助!

下面是代码:

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
    .Cells(1).Select
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False
    '.Cells(1).Select
    '.Cells(1).EntireRow.AutoFit works but i don't have specific width to choose
    '.Cells(1).EntireColumn.AutoFit
    '.Columns(1).ColumnWidth = 10.45 doesn't work
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
     SourceType:=xlSourceRange, _
     Filename:=TempFile, _
     Sheet:=TempWB.Sheets(1).Name, _
     Source:=TempWB.Sheets(1).UsedRange.Address, _
     HtmlType:=xlHtmlStatic)
    .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
rangetoHTML = ts.readall
ts.Close
rangetoHTML = Replace(rangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

End Function
excel vba outlook html-email
1个回答
0
投票

您可以在调用函数之前在源范围内设置列宽。

Sub Set_Col_Width()
    ActiveWorkbook.ActiveSheet.Columns("B:B").ColumnWidth = 20
    ActiveWorkbook.ActiveSheet.Columns("D:D").ColumnWidth = 10
End Sub

该函数生成的 HTML 使用您设置的宽度:

  <td class=xl154597 width=150 style='width:112pt'>Column B</td>
  <td class=xl154597 width=78 style='width:58pt'>Column D</td>
© www.soinside.com 2019 - 2024. All rights reserved.