简体   繁体   English

使用 VBA 以编程方式更改 Outlook 中电子邮件正文中的属性

[英]Programmatically change properties in email body in Outlook with VBA

I have an email ready to be sent in Outlook 2013 I want to scan the body of the email for bold text (ie, bold characters) and change its color to red (nice to have) Exclude from the macro the signature我有一封准备在 Outlook 2013 中发送的电子邮件 我想扫描电子邮件正文中的粗体文本(即粗体字符)并将其颜色更改为红色(很好) 从宏中排除签名

I put together the code below but still not working.我把下面的代码放在一起,但仍然无法正常工作。 Any ideas?有任何想法吗?

Public Sub FormatSelectedText()
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector

    ' Add reference to Word library
    ' in VBA Editor, Tools, References
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next

    'Reference the current Outlook item
    Set objItem = Application.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then

                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection
                Set objChar = Characters.Selection

                ' replace the With block with your code
                   With objChar
                   ' Formatting code goes here
                        '.Font.Size = 18
                        If .Font.Bold = True Then
                            .Font.Color = wdColorBlue
                        End If
                        .Font.Color = wdColorRed
                        '.Font.Italic = True
                        '.Font.Name = "Arial"
                   End With

                 For Each Char In Characters.Selection
                     If Char.Font.Bold Then
                        Char.Font.Color = RGB(0, 0, 255) 'TextRGBTmp
                     End If
                 Next Char

                 For Each Char In Characters.Selection
                     If Not Char.Font.Bold And Char.Font.Color = RGB(0, 0, 255) Then
                        Char.Font.Color = RGB(0, 0, 0)
                     End If
                 Next Char


            End If
        End If
    End If


    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub

This is a follow up to question: Programmatically change font properties in email body这是问题的后续:以编程方式更改电子邮件正文中的字体属性

first of all: don't use On Error Resume Next when you're trying to debug your code.首先:当您尝试调试代码时,不要使用On Error Resume Next It makes your life harder.它让你的生活更加艰难。

second: use Option Explicit at the beginning of the module.第二:在模块的开头使用Option Explicit With that option enabled, VBA will show you every variable that's not initialized (some bugs only occur from misspellings).启用该选项后,VBA 将显示每个未初始化的变量(某些错误仅因拼写错误而发生)。

I've corrected your code, so it works for me:我已经更正了你的代码,所以它对我有用:

Public Sub FormatSelectedText()
    Dim objOutlook As Outlook.Application ' i used this because im working in MS Access
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector

    ' Add reference to Word library
    ' in VBA Editor, Tools, References
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim objChar As Object
    Dim Char As Object

    'Reference the current Outlook item
    Set objOutlook = GetObject(, "Outlook.Application")
    Set objItem = objOutlook.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then

                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection
                Set objChar = objSel.Characters ' this wasn't initialized

                ' replace the With block with your code
'                   With objChar ' you don't Need this block because objChar is an array and it throws an error when you try to use this code on the whole objChar object
'                   ' Formatting code goes here
'                        '.Font.Size = 18
'                        If .Font.Bold = True Then
'                            .Font.color = wdColorBlue
'                        End If
'                        .Font.color = wdColorRed
'                        '.Font.Italic = True
'                        '.Font.Name = "Arial"
'                   End With

                 For Each Char In objSel.Characters
                     If Char.Font.Bold Then
                        Char.Font.color = rgb(255, 0, 0) 'TextRGBTmp (the rgb was filled backwards, so the text became blue. i fixed it.
                     End If
                 Next Char
' the code of the second For Each was not neccessary.

            End If
        End If
    End If


    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM