使用VBA将从网站复制的表粘贴到Outlook电子邮件的正文中



我有这样的代码,它创建一封电子邮件,填写收件人、抄送、主题字段;我想让它给我从互联网上手动复制的表格发短信。所以基本上我希望它在电子邮件的正文中做一个ctrl-v。我试了一下doclipboard。GetText方法和其他一些方法。这是最近的一次尝试。

Sub SendEmail(ByVal strSubject As String, ByVal strBody As String, ByVal blnDisplay As Boolean, ByVal blnAddPaste As Boolean)
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
'Set the recipient for the new email
.To = "Email Addresses"
'Set the recipient for a copy
'.CC = strCC
'Set the subject
.Subject = "Salesforce " & EmailDateFormat & " " & strSubject
'The content of the document is used as the body for the email
If blnAddPaste Then
.HTMLBody = strBody & "<br><br>" & _ 
Selection.PasteAndFormat(Word.WdRecoveryType.wdTableOriginalFormatting) ' this is what I need to fix
Else
.HTMLBody = strBody
End If


If blnDisplay Then
.Display
Else
.Send
End If
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub

如何在outlook电子邮件中粘贴表格?

我为其他寻找解决方案的人找到了它。

Sub SendEmail(ByVal strSubject As String, ByVal strBody As String, ByVal blnDisplay As Boolean, ByVal blnAddPaste As Boolean)
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim oWordDoc As Word.Document
Dim oWordRange As Word.Range
Dim VarPosition As Variant
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
If blnDisplay Then
.Display
End If
Set oWordDoc = oItem.GetInspector.WordEditor
'Set the recipient for the new email
.To = "Email Addresses"
'Set the recipient for a copy
'.CC = strCC
'Set the subject
.Subject = "Salesforce " & EmailDateFormat & " " & strSubject
'The content of the document is used as the body for the email
If blnAddPaste Then
.Body = strBody  '& Selection.PasteAndFormat(Word.WdRecoveryType.wdTableOriginalFormatting)
VarPosition = oWordDoc.Range.End - 1
Set oWordRange = oWordDoc.Range(VarPosition, VarPosition)
oWordRange.Select
oWordRange.PasteAndFormat (Word.WdRecoveryType.wdFormatOriginalFormatting)
'SendKeys ("^v")
Else
.HTMLBody = strBody
End If


If Not blnDisplay Then
.Send
End If
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub

最新更新