将格式化的 Excel 范围粘贴到 Outlook 任务中
Paste formatted Excel range into Outlook task
我一直在尝试创建一个子程序,它会从 Excel 选择中获取一些信息并在 Outlook 上创建一个新任务。任务的主体应该包含来自第一个单元格的注释(它已经这样做了),但在我想要粘贴范围之前,它看起来像 Excel,然后是注释,然后是范围。
这是我的代码:
Sub CreateReminder()
Dim olApp As Object
Dim olRem As Object
Dim myRange As Range
Dim contact As String
Dim company As String
Dim city As String
Dim state As String
Dim cmt As comment
Dim comment As String
Dim strdate As Date
Dim remdate As Date
Set olApp = CreateObject("Outlook.Application")
Set olRem = olApp.CreateItem(3)
Set myRange = Selection
If ActiveCell.comment Is Nothing Then
Exit Sub
Else
Set cmt = ActiveCell.comment
End If
company = myRange.Columns(1).Text
contact = myRange.Columns(2).Text
If InStr(contact, "/") <> 0 Then
contact = Left(contact, InStr(contact, "/") - 1)
End If
city = myRange.Columns(7).Text
state = myRange.Columns(8).Text
myRange.Copy
comment = cmt.Text
strdate = Date
remdate = Format(Now)
rangeaddress = myRange.Address
wrksheetname = ActiveSheet.Name
With olRem
.Subject = "Call " & contact & " - " & company & " - " & city & ", " & state
.display
SendKeys "{TAB 9}"
SendKeys "^{v}"
.body = Chr(10) & comment & Chr(10)
'.startdate = strdate
'.remindertime = remdate
'.reminderset = True
'.showcategoriesdialog
End With
Set olApp = Nothing
Set olRem = Nothing
End Sub
如您所见,我可以使用 SendKeys 方法进行粘贴,但这有点像 hack,并不...复杂。我确定还有另一种方法,有什么想法吗?
我找到了将 HTML 粘贴到电子邮件的代码,但据我所知,邮件项目允许 HTML,但任务项目不允许。
Outlook 使用 Word 作为电子邮件编辑器。您可以使用 Word 对象模型对邮件正文进行操作。 Inspector class returns 的 WordEditor 属性 Document class 的实例(来自 Word 对象模型)表示正文。您可以在 Chapter 17: Working with Item Bodies 中阅读有关该方式和所有可能方式的更多信息。
这样您就可以使用范围 class 的 Copy 方法将范围复制到剪贴板。然后您可以使用 Word 对象模型中的粘贴方法将数据粘贴到代表邮件正文的文档中。
我一直在尝试创建一个子程序,它会从 Excel 选择中获取一些信息并在 Outlook 上创建一个新任务。任务的主体应该包含来自第一个单元格的注释(它已经这样做了),但在我想要粘贴范围之前,它看起来像 Excel,然后是注释,然后是范围。
这是我的代码:
Sub CreateReminder()
Dim olApp As Object
Dim olRem As Object
Dim myRange As Range
Dim contact As String
Dim company As String
Dim city As String
Dim state As String
Dim cmt As comment
Dim comment As String
Dim strdate As Date
Dim remdate As Date
Set olApp = CreateObject("Outlook.Application")
Set olRem = olApp.CreateItem(3)
Set myRange = Selection
If ActiveCell.comment Is Nothing Then
Exit Sub
Else
Set cmt = ActiveCell.comment
End If
company = myRange.Columns(1).Text
contact = myRange.Columns(2).Text
If InStr(contact, "/") <> 0 Then
contact = Left(contact, InStr(contact, "/") - 1)
End If
city = myRange.Columns(7).Text
state = myRange.Columns(8).Text
myRange.Copy
comment = cmt.Text
strdate = Date
remdate = Format(Now)
rangeaddress = myRange.Address
wrksheetname = ActiveSheet.Name
With olRem
.Subject = "Call " & contact & " - " & company & " - " & city & ", " & state
.display
SendKeys "{TAB 9}"
SendKeys "^{v}"
.body = Chr(10) & comment & Chr(10)
'.startdate = strdate
'.remindertime = remdate
'.reminderset = True
'.showcategoriesdialog
End With
Set olApp = Nothing
Set olRem = Nothing
End Sub
如您所见,我可以使用 SendKeys 方法进行粘贴,但这有点像 hack,并不...复杂。我确定还有另一种方法,有什么想法吗?
我找到了将 HTML 粘贴到电子邮件的代码,但据我所知,邮件项目允许 HTML,但任务项目不允许。
Outlook 使用 Word 作为电子邮件编辑器。您可以使用 Word 对象模型对邮件正文进行操作。 Inspector class returns 的 WordEditor 属性 Document class 的实例(来自 Word 对象模型)表示正文。您可以在 Chapter 17: Working with Item Bodies 中阅读有关该方式和所有可能方式的更多信息。
这样您就可以使用范围 class 的 Copy 方法将范围复制到剪贴板。然后您可以使用 Word 对象模型中的粘贴方法将数据粘贴到代表邮件正文的文档中。