我正在使用 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
uj5u.com熱心網友回復:
您可以在呼叫函式之前設定源范圍中的列寬。
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/530314.html
上一篇:檢查Excel是否為空
下一篇:如何在ansible中使用npm
