【问题标题】:Exporting rich text to outlook and keep formatting将富文本导出到 Outlook 并保持格式
【发布时间】:2014-08-06 08:42:36
【问题描述】:

我在 Access 中有一个按钮,可以打开 Outlook,创建约会。

Private Sub addAppointEstimate_Click()
    Dim objOutlook As Object
    Dim objOutLookApp As Object
    Dim strSubject As String
    Dim strBody As String

    strSubject = Forms!frmMain.LastName 'more stuff to add
    strBody = DLookup("EstimateText", "tblEstimateItems", "EstimateID = 78") '& Forms!frmMain!frmSubTransaction!frmSubEstimate.Form.EstimateID)

    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutLookApp = objOutlook.CreateItem(1)
    With objOutLookApp
        .subject = strSubject
        .RTFBody = StrConv(strBody, vbFromUnicode)
        .Display
    End With

End Sub

问题是我想在正文中插入富文本,但格式不正确,因为它显示了所有 HTML 标记,例如:

<div><strong>example </strong><font color=red>text</font></div>

有没有办法可以将富文本以 Outlook 可以识别的格式发送或转换到 Outlook?(可能使用剪贴板)

似乎很多人都有 Excel 解决方案,但我正在努力让他们在 Access 中工作:

【问题讨论】:

  • strBody 真的是 RTF 格式的字符串还是 HTML?在后一种情况下,只需设置 HTMLBody 属性。

标签: vba ms-access outlook


【解决方案1】:

将 RTF 格式的字符串传递给 Outlook 电子邮件正文很简单,如下所示

Function RTF2Outlook(strRTF as String) as boolean
    Dim myOlApp, myOlItem
    Dim arrFiles() As String, arrDesc() As String, i As Long

    Set myOlApp = CreateObject("Outlook.Application")
    Set myOlItem = myOlApp.CreateItem(olMailItem)

    With myOlItem
       .BodyFormat = olFormatRichText
       .Body = StrConv(strRTF, vbFromUnicode) 'Convert RTF string to byte array
    End With
    Set myOlApp = Nothing
    Set myOlItem = Nothing
End Function

秘诀是不要使用“.RTFBody”,而只是使用“.Body”并将字节数组传递给它,就像上面的代码一样。我花了一段时间才弄清楚。 感谢微软,我们总能找到一些东西。

【讨论】:

  • 感谢您使用 .Body 而不是 .RTFBody 的观察。我已经工作了 2 天,终于它似乎工作了。
【解决方案2】:

您可以使用一些额外的开销来创建带有格式化 HTMLBody 内容的消息,然后将内容复制到约会项目。

首先创建一条消息和一个约会,然后根据需要填充它们。将正文放入消息中,暂时跳过约会中的正文。

Dim objOutlook As Object
Dim objMyMsgItem As Object
Dim objMyApptItem As Object
Dim strSubject As String

strSubject = "Some text" 'Forms!frmMain.LastName 'more stuff to add

Set objOutlook = CreateObject("Outlook.Application")
Set objMyMsgItem = objOutlook.CreateItem(0) 'Message Item
With objMyMsgItem
    .HTMLBody = "<div><strong>example </strong><font color=red>text</font></div>"
            'DLookup("EstimateText", "tblEstimateItems", "EstimateID = 78")
    .Display
End With

Set objMyApptItem = objOutlook.CreateItem(1) 'Appointment Item
With objMyApptItem
    .Subject = strSubject
    .Display
End With

然后使用 GetInspector 属性通过 Word 编辑器与每个项目的正文进行交互,并以这种方式复制格式化的文本。

Dim MyMsgInspector As Object
Dim wdDoc_Msg As Object
Set MyMsgInspector = objMyMsgItem.GetInspector
Set wdDoc_Msg = MyMsgInspector.WordEditor

Dim MyApptInspector As Object
Dim wdDoc_Appt As Object
Set MyApptInspector = objMyApptItem.GetInspector
Set wdDoc_Appt = MyApptInspector.WordEditor

wdDoc_Appt.Range.FormattedText = wdDoc_Msg.Range.FormattedText

此代码经过测试,可在 Access 2013 中使用。

【讨论】:

  • 我可能错了,但我发誓最后一个 GetInspector 代码块没有做任何事情?
  • 尝试添加对 Microsoft Word 14.0 对象库的引用(在 Visual Basic 编辑器中,单击工具菜单下的引用,向下滚动并选中相应的框)。
  • Hacky 但工作得很好,谢谢。我正在使用 JavaScript 版本: var apptIns =ointment.GetInspector(); var msgIns = msg.GetInspector(); var apptDoc = apptIns.WordEditor; var msgDoc = msgIns.WordEditor; apptDoc.Range().FormattedText = msgDoc.Range().FormattedText;
【解决方案3】:

您正在设置纯文本 Body 属性。将 HTMLBody 属性设置为格式正确的 HTML 字符串。

【讨论】:

  • 该对象显然不支持 .HTMLBody 属性。而且我认为我也不能更改约会的 .BodyFormat。
  • Outlook 约会、任务和联系人确实不支持 HTMLBody 属性,它仅由 MailItem 对象公开。您可以将 RtfBody 属性(字节数组)设置为格式正确的 RTF 数据。如果使用 Redemption 是一个选项,它会在 RDOAppointmentItem、RDOContactItem 和 RDOTaskItem 对象上公开 HTMLBody 属性 - 在运行时 Redemption 将指定的 HTML 动态转换为 RTF。
  • 好的,我已经尝试将 RTFBody 属性设置为我的文本。我想我正确地将它转换为字节数组(请参阅上面的更新代码)。但我收到对象“_Appointment”的错误消息“RTFBody”失败。有什么想法吗?
  • 也不,StrConv 将一个字符串转换为另一个字符串。你需要一个字节分析(如 Dim SomeVar(100) 等)。另外,它必须是真正的 RTF 文本 - 在记事本中打开现有的 RTF 文件。
  • 我正在研究一种解决方案,该解决方案涉及将格式化文本复制到剪贴板,然后将其粘贴到约会正文中。我知道我可能应该将此作为一个新问题发布,但是是否可以在访问中使用 VBA 粘贴到正文中?
【解决方案4】:

我想出了一个解决方案。我刚刚复制并粘贴了整个子,但我保证答案就在那里。我还强调了重要的部分。

我在我的家用机器上工作,但不在客户端上工作。所以 不能使用它,但如果你能改进它,请告诉我。

Private Sub addAppointmentEst_Click()


    Dim objOutlook As Object
    Dim objOutLookApp As Object
    Dim strSubject As String
    Dim strBody As String

    On Error GoTo appointmentEstError

    If Not IsNull(DLookup("EstimateID", "tblEstimate", "TransactionID = " & Me.TransactionID.Value)) Then
        DoCmd.OpenForm "frmEditEstimate", , , , , acHidden '<------ OPEN FORMATTED TEXT IN A FORM
        Forms!frmEditEstimate.SetFocus
        Forms!frmEditEstimate!frmSubEstimateItems.Form.EstimateText.SetFocus
        DoCmd.RunCommand acCmdCopy '<------ COPY FORMATTED TEXT
        DoCmd.Close acForm, "frmEditEstimate", acSaveNo
    End If

'        If Not IsNull(Forms!frmMain.Title.Value) Then
'            strSubject = strSubject & Forms!frmMain.Title.Value
'        End If
     If Not IsNull(Forms!frmMain.FirstName.Value) Then
         strSubject = strSubject & Forms!frmMain.FirstName.Value
    End If
    If Not IsNull(Forms!frmMain.LastName.Value) Then
        strSubject = strSubject & " " & Forms!frmMain.LastName.Value
    End If
    If Not IsNull(Forms!frmMain.Organisation.Value) Then
        strSubject = strSubject & " (" & Forms!frmMain.Organisation.Value & ")"
    End If
    If Not IsNull(Forms!frmMain!frmSubTransaction.Form.Property.Value) Then
        strSubject = strSubject & " - " & Forms!frmMain!frmSubTransaction.Form.Property.Value
    End If

    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutLookApp = objOutlook.CreateItem(1)

     With objOutLookApp
         .subject = strSubject
         .Display
     End With

    If Not IsNull(DLookup("EstimateID", "tblEstimate", "TransactionID = " & Me.TransactionID.Value)) Then
        Set objectOutlookBody = objOutlook.ActiveInspector.WordEditor
        objOutLookApp.Body = vbCrLf & "Estimate ID: " & Forms!frmMain!frmSubTransaction!frmSubEstimate.Form.EstimateID.Value & _
                            vbCrLf & "Estimate Date: " & Forms!frmMain!frmSubTransaction!frmSubEstimate.Form.EstimateDate.Value
        objectOutlookBody.Application.Selection.Paste '<----- PASTE TEXT INTO APPOINTMENT

        Forms!frmMain.EmptyValue.Value = " " '<----- EMPTY CLIPBOARD
        Forms!frmMain.EmptyValue.SetFocus
        DoCmd.RunCommand acCmdCopy
    End If

Exit Sub

appointmentEstError:
        MsgBox _
        Prompt:="Failed create an appointment in Outlook, with the estimate attached", _
        Buttons:=vbOKOnly + vbExclamation, _
        Title:="Error"
End Sub

【讨论】:

    【解决方案5】:

    和之前的回答一样,这一行是关键,它复制文本、超链接、图片等而不修改剪贴板内容:

    wdDoc_Appt.Range.FormattedText = wdDoc_Msg.Range.FormattedText
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多