在Excel中通过电子邮件发送单个工作表,并粘贴值而不是公式
我使用下面的代码实现了一个宏,它创建一封电子邮件,将活动工作表发送给某人 资料来源: 它设置电子邮件没有问题,但不幸的是,复制的工作表中某些单元格的内容是“#REF”,而不是原始工作表中单元格中包含的内容。这只发生在一些细胞上,我不知道为什么。原始工作表中为空的单元格在新工作表中始终显示“#REF”在Excel中通过电子邮件发送单个工作表,并粘贴值而不是公式,excel,vba,email,excel-2007,Excel,Vba,Email,Excel 2007,我使用下面的代码实现了一个宏,它创建一封电子邮件,将活动工作表发送给某人 资料来源: 它设置电子邮件没有问题,但不幸的是,复制的工作表中某些单元格的内容是“#REF”,而不是原始工作表中单元格中包含的内容。这只发生在一些细胞上,我不知道为什么。原始工作表中为空的单元格在新工作表中始终显示“#REF” Sub Mail_ActiveSheet() Dim FileExtStr As String Dim FileFormatNum As Long Dim Sourcewb As Workbook
Sub Mail_ActiveSheet()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
With Destwb
If Val(Application.Version) < 12 Then
' You are using Excel 97-2003.
FileExtStr = ".xls": FileFormatNum = -4143
Else
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is No in the security dialog."
Exit Sub
Else
Select Case Sourcewb.FileFormat
' Code 51 represents the enumeration for a macro-free
' Excel 2007 Workbook (.xlsx).
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
' Code 52 represents the enumeration for a
' macro-enabled Excel 2007 Workbook (.xlsm).
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
' Code 56 represents the enumeration for a
' a legacy Excel 97-2003 Workbook (.xls).
Case 56: FileExtStr = ".xls": FileFormatNum = 56
' Code 50 represents the enumeration for a
' binary Excel 2007 Workbook (.xlsb).
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
' Change all cells in the worksheet to values, if desired.
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
'Save the new workbook and then mail it.
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
For I = 1 To 3
.SendMail "insert@emailhere", _
"This is the Subject line"
If Err.Number = 0 Then Exit For
Next I
On Error GoTo 0
.Close SaveChanges:=False
End With
' Delete the file you just sent.
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Sub-Mail\u-ActiveSheet()
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
Dim TempFilePath作为字符串
将文件名设置为字符串
应用
.ScreenUpdate=False
.EnableEvents=False
以
设置Sourcewb=ActiveWorkbook
活动表,收到
设置Destwb=ActiveWorkbook
用Destwb
如果Val(Application.Version)<12,则
'您正在使用Excel 97-2003。
FileExtStr=“.xls”:FileFormatNum=-4143
其他的
如果Sourcewb.Name=.Name,则
应用
.ScreenUpdate=True
.EnableEvents=True
以
MsgBox“您的答案在安全对话框中为否。”
出口接头
其他的
选择Case Sourcewb.FileFormat
'代码51表示无宏的枚举
'Excel 2007工作簿(.xlsx)。
案例51:FileExtStr=“.xlsx”:FileFormatNum=51
'代码52表示
'启用宏的Excel 2007工作簿(.xlsm)。
案例52:
如果.hasvb项目
FileExtStr=“.xlsm”:FileFormatNum=52
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
'代码56表示
'旧版Excel 97-2003工作簿(.xls)。
案例56:FileExtStr=“.xls”:FileFormatNum=56
'代码50表示
'二进制Excel 2007工作簿(.xlsb)。
其他情况:FileExtStr=“.xlsb”:FileFormatNum=50
结束选择
如果结束
如果结束
以
'如果需要,将工作表中的所有单元格更改为值。
与Destwb.Sheets(1)一起使用
.细胞,复制
.Cells.paste特殊XLPaste值
.单元格(1)。选择
以
Application.CutCopyMode=False
'保存新工作簿,然后将其邮寄。
TempFilePath=Environ$(“temp”)和“\”
TempFileName=“部分”&Sourcewb.Name&“&Format(现在是“dd-mmm-yy h-mm-ss”)
用Destwb
.SaveAs TempFilePath&TempFileName&FileExtStr,FileFormat:=FileFormatNum
出错时继续下一步
对于I=1到3
.SendMail“insert@emailhere", _
“这是主题行”
如果Err.Number=0,则退出
接下来我
错误转到0
.Close SaveChanges:=False
以
'删除刚发送的文件。
终止TempFilePath&TempFileName&FileExtStr
应用
.ScreenUpdate=True
.EnableEvents=True
以
端接头
手动将工作表复制到新工作簿会导致相同的问题。在设置Sourcewb=ActiveWorkbook之前,我用以下代码修复了它
Cells.Select
Selection.Copy
Workbooks.Add
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
然后在TempFileName=
Sourcewb.Close SaveChanges:=False
黑暗中的一枪。不要执行
Destwb.Sheets(1)。在执行ActiveSheet.Copy
之前,使用drange.Cells.Copy和paste
do。复制和粘贴
。您可以随时关闭该文件,而不必稍后保存。