复制范围并将其作为Outlook电子邮件主体发送



i具有以下VBA代码,可以在Excel中复制范围,并将其作为来自Outlook的电子邮件主体发送。Excel表可保存在服务器中,并且在我的计算机和其他一些计算机/笔记本电脑中也可以很好地工作。但是它给其他某些用户提供了编译错误。所有用户都连接到同一网络,并使用相同版本的Outlook和Excel 2016。

编译错误 - 找不到项目或库

当我单击同事(用户(的其他计算机运行此脚本的命令按钮时,该错误在线提到的(此处是错误(。Outlook和Excel是2016年。我在参考菜单中添加了Outlook对象库。Excel表位于服务器文件夹\hp-fs2GroupsH&P ISMNavigational Assessment上任何解决该论坛专家的代码的解决方案都将不胜感激。

Private Sub CommandButton1_Click()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Sheet1.Unprotect ("so")
Set rng = Nothing
On Error Resume Next
Set rng = Sheets("Sheet1").Range("B9:I22").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
    .To = "abcdg@abcd.com"
    .CC = ""
    .BCC = ""
    .Subject = "This is Automatic Notification - Navigation Assessment Overview sheet has just been updated !"
    .HTMLBody = RangetoHTML(rng)
    '.Send
    .display
End With
On Error GoTo 0
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Sheet1.Protect ("so")
End Sub
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"
'( ERROR HERE - at the line TempFile = Environ$ )
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
'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 used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

我对临时文件感兴趣...也许其他用户没有适当的凭据可以保存到其计算机上的环境$?还是环境$指向某些受限制的网络位置?

我敢打赌,如果您在我的文档下的用户计算机上进行临时文件 yourproject temp(或某些等效(,则此问题会消失。

最新更新