2014-06-16 122 views
1

我在Access中有一個打開Outlook的按鈕,用於創建約會。將富文本導出到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?(也許使用剪貼板)

似乎很多人都解決了Excel中,但我努力讓他們在獲得工作:

+0

strBody是一個真正的RTF格式的字符串或HTML?在後一種情況下,只需設置HTMLBody屬性即可。 –

回答

0

工作,我想出了一個解決方案。我剛剛複製並粘貼了整個子文件,但答案在那裏,我保證。我也強調了重要的一點。

我在我的家用機器上工作,但不在客戶機上工作。所以不能使用它,但如果你能改善它,讓我知道。

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 
0

您正在設置純文本Body屬性。將HTMLBody屬性設置爲格式正確的HTML字符串。

+0

該對象顯然不支持.HTMLBody屬性。我不認爲我可以改變。BodyFormat的預約。 – Magnus

+0

Outlook約會,任務和聯繫人確實不支持HTMLBody屬性,它僅由MailItem對象公開。您可以將RtfBody屬性(字節數組)設置爲格式正確的RTF數據。如果使用Redemption是一個選項,它會暴露RDOAppointmentItem,RDOContactItem和RDOTaskItem對象上的HTMLBody屬性 - 在運行時,Redemption會動態地將指定的HTML轉換爲RTF。 –

+0

好吧我已經嘗試將RTFBody屬性設置爲我的文本。我想我正確地將它轉換成Byte數組(請參閱上面的更新代碼)。但是我收到對象「_Appointment」的錯誤消息「RTFBody」失敗。有任何想法嗎? – Magnus

1

您可以使用一些額外開銷創建具有格式化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 

此代碼測試,在訪問2013年

+0

我可能是錯的,但我發誓最後一個GetInspector代碼塊沒有做任何事情? – Magnus

+0

嘗試添加對Microsoft Word 14.0對象庫的引用(在Visual Basic編輯器中,單擊工具菜單下的引用,向下滾動並選中相應的框)。 – AjimOthy

+0

哈克,但非常感謝。我正在使用JavaScript版本:var apptIns = appointment.GetInspector(); var msgIns = msg.GetInspector(); var apptDoc = apptIns.WordEditor; var msgDoc = msgIns.WordEditor; apptDoc.Range()。FormattedText = msgDoc.Range()。FormattedText; – WheretheresaWill

0

正如在前面的回答,這條線是關鍵,它複製文本,超鏈接,圖片等,而無需修改剪貼板中的內容:

wdDoc_Appt.Range.FormattedText = wdDoc_Msg.Range.FormattedText 
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(strBody, vbFromUnicode) 'Convert RTF string to byte array 
    End With 
    Set myOlApp = Nothing 
    Set myOlItem = Nothing 
End Function 

祕密不使用「.RTFBody」只是「體」和傳遞給它的字節數組如在上面的代碼。我花了一段時間才弄明白。 感謝Microsoft,我們總是會找出一些想法。

+0

感謝您使用.Body而不是.RTFBody的觀察。我已經工作了2天,最後它似乎工作。 – Marichyasana

相關問題