使用VBA以编程方式更改Outlook中电子邮件正文中的属性 [英] Programmatically change properties in email body in Outlook with VBA

查看:2684
本文介绍了使用VBA以编程方式更改Outlook中电子邮件正文中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件准备在Outlook 2013中发送
我想扫描电子邮件的正文,用于显示粗体文字(即粗体字符),并将其颜色更改为红色
(很高兴有)从宏中排除签名



我将下面的代码放在一起,但仍然无法正常工作。任何想法?

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

'在VBA编辑器,工具,参考文献中添加对Word库
'的引用
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next

'引用当前的Outlook项目
设置objItem = Application.ActiveInspector.CurrentItem
如果没有objItem是没有的
如果objItem.Class = olMail然后
设置objInsp = objItem.GetInspector
如果objInsp.EditorType = olEditorWord然后

设置objDoc = objInsp.WordEditor
设置objWord = objDoc.Application
设置objSel = objWord.Selection
设置objChar = Characters.Selection

'用代码替换With块
使用objChar
'格式化代码到这里
'.Font.Size = 18
如果.Font.Bold = True然后
.Font.Color = wdColorBlue
结束If
.Font.Color = wdColorRed
'.Font.Italic = True
'.Font.Name =Arial
结束

对于每个Char In Characters.Selection
如果Char.Font.Bold然后
Char.Font.Color = RGB(0,0,255)'TextRGBTmp
如果
下一个Char

对于Char CharS中的每个Char CharS.Font.Color = RGB(0,0,0)
结束If
下一页Ch ar


结束If
如果
结束If


设置objItem = Nothing
设置objWord =没有
设置objSel =没有
设置objInsp =没有
结束Sub

这是一个后续问题:以编程方式更改电子邮件正文中的字体属性

解决方案

首先:不要使用 On Error Resume Next 当您尝试调试您的代码。这使得你的生活更加困难。



第二个:在模块的开头使用 Option Explicit 启用该选项后,VBA将显示每个未初始化的变量(一些错误仅发生在拼写错误中)。



我已经更正了代码,因此它适用于我:

  Public Sub FormatSelectedText()
Dim objOutlook As Outlook.Application'我使用这个,因为在MS Access中工作
Dim objItem As Object
Dim objInsp As Outlook.Inspector

'在VBA编辑器,工具,参考文献中添加对Word库
'的引用
Dim objWord作为Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Dim objChar As Object
Dim Char As Object

'Reference当前的Outlook项目
设置objOutlook = GetObject(,Outlook.Application)
设置objItem = objOutlook.ActiveInspector.CurrentItem
如果没有objItem是没有然后
如果objItem.Class = olMail然后
设置objInsp = objItem.GetInspector
如果objInsp.EditorType = olEditorWord然后

设置objDoc = objInsp.WordEditor
设置objWord = objDoc.Application
设置objSel = objWord.Selection
设置objChar = objSel.Characters'这没有初始化

'用你的代码替换With块
'使用objChar'你不需要这个块,因为objChar是一个数组,它抛出当您尝试在整个objChar对象上使用此代码时出现错误
''格式化代码到这里
''.Font.Size = 18
'If .Font.Bold = True Then
'.Font.color = wdColorBlue
'End If
'.Font.color = wdColorRed
''.Font.Italic = True
''.Font.Name =Arial
结束

对于每个字符在objSel.Characters
如果Char.Font.Bold然后
Char.Font.color = rgb(255,0,0)'TextRGBTmp( rgb向后填充,所以文本变成蓝色。我修好了它。
结束如果
下一个Char
'第二个代码For Each不是必需的。

End If
End If
End If


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


I have an email ready to be sent in Outlook 2013 I want to scan the body of the email for bold text (i.e., bold characters) and change its color to red (nice to have) Exclude from the macro the signature

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. It makes your life harder.

second: use Option Explicit at the beginning of the module. With that option enabled, VBA will show you every variable that's not initialized (some bugs only occur from misspellings).

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

这篇关于使用VBA以编程方式更改Outlook中电子邮件正文中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆