利用vba把excel里内容转到outlook邮件格式问题
我利用下面这段代码将excel里A1:B10区域的内容通过下面这段代码从excel转到outlook邮件里,但有一个问题,当内容转到outlook粘贴内容之后发现exce...
我利用下面这段代码将excel里A1:B10区域的内容通过下面这段代码从excel转到outlook邮件里,但有一个问题,当内容转到outlook粘贴内容之后发现excel里选中的区域被粘贴到了outlook里是从第二行开始的,第一行是空的。
Public Function RangetoHTML(rng As Range)
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"
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
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
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=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
还有一个问题就是当excel里A1:B10这个区域里某个单元格出现多行内容时,这个单元格转到outlook里之后行与行之间也会空一行出来,比如在excel里的A2单元格的内容为三行,当这个单元格转到outlook之后内容就会变成5行,行与行之间出现一个空行。
excel里样式:
kljlkjlkjljlkjlkj
jkljljljlkljkl
转到outlook的格式为:
kljlkjlkjljlkjlkj
jkljljljlkljkl 展开
Public Function RangetoHTML(rng As Range)
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"
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
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
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=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
还有一个问题就是当excel里A1:B10这个区域里某个单元格出现多行内容时,这个单元格转到outlook里之后行与行之间也会空一行出来,比如在excel里的A2单元格的内容为三行,当这个单元格转到outlook之后内容就会变成5行,行与行之间出现一个空行。
excel里样式:
kljlkjlkjljlkjlkj
jkljljljlkljkl
转到outlook的格式为:
kljlkjlkjljlkjlkj
jkljljljlkljkl 展开
1个回答
展开全部
第二个问题多行这块
在VB里进行导出前,将单元格自动换行功能关闭.
就不会导出换行符
在VB里进行导出前,将单元格自动换行功能关闭.
就不会导出换行符
追问
麻烦再问如何关闭这个功能?烦请解答。谢谢!~
追答
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
主要问题在excel的publishobject
这个转换网页对象只导出列宽内出现的字符.
单元格多出的字符会被舍弃.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询