How to edit existing MailItem using WordEditor in Outlook VBA?

前端 未结 1 1384
遇见更好的自我
遇见更好的自我 2021-01-22 01:39

So I want to edit my received mails afterwards to add links. If the emails have been received as plain text or HTML I have just edited the appropriate msg.Body or msg.HTMLBody.

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 02:31

    I was facing exactly the same issue (Outlook VBA: Replace inline object with text). As posted in my comment (soon to be edited to a more polished version, after further testing), you have to use objDoc.UnProtect prior to modifying contents. I have actually used

    'On Error Resume Next   ' This will prevent the error message... risky!
    Dim odProt As Integer
    odProt = objDoc.ProtectionType
    If (odProt <> wdNoProtection) Then
        Debug.Print "Document is protected with type " & odProt & ", unprotecting temporarily"
        objDoc.UnProtect
    End If
    ' ... Write your code
    If (odProt <> wdNoProtection) Then
        Debug.Print "Restoring protection"
        objDoc.Protect (odProt)
    End If
    objMsg.Display
    objMsg.Save
    

    I am not sure if the last two lines are the best, but it worked for me.

    Note that having On Error Resume Next will prevent the error message, and you may see that none of your editions has any effect without apparent reason.

    0 讨论(0)
提交回复
热议问题