【问题标题】:Populating the subject line of an email with the first line, of its body, that contains text使用包含文本的正文的第一行填充电子邮件的主题行
【发布时间】:2019-07-30 17:56:48
【问题描述】:

我办公室的团队花费大量时间将一篇文章的第一行复制粘贴到正文中,然后粘贴到主题行中。

我找到了一个解决方案,将正文的第一行设置为主题。
问题是正文的第一行文本上方总是有两到三个空白行。 解决方案设置主题为" ".

有没有办法删除顶部的空行,或者跳过它们并将主题设置为文本的第一行?

DataNumen 的 Shirley Zhang 提供了代码。

我一直在使用的 VBA 代码:

Private WithEvents objInspectors As Outlook.Inspectors

Private Sub Application_Startup()
   Set objInspectors = Outlook.Application.Inspectors
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olMail And Inspector.CurrentItem.subject = "" Then
       Inspector.CurrentItem.subject = " "
    End If
End Sub

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objMail As Outlook.MailItem
Dim objMailDocument As Word.Document
Dim objMailSelection As Word.Selection

If TypeOf Item Is MailItem Then
    Set objMail = Item

    If Len(Trim(objMail.subject)) = 0 Then
        Set objMailDocument = objMail.GetInspector.WordEditor
        Set objMailSelection = objMailDocument.Application.Selection

        objMailDocument.Range(0, 0).Select
        objMailSelection.MoveEnd wdLine

        'Take first line of body as subject
        objMail.subject = objMailSelection.Text
    End If
End If
End Sub

【问题讨论】:

  • 我无法测试,但可以尝试在第一行之后再添加几行 objMailSelection.MoveEnd wdLine 行?
  • 它有效,但仅在某些情况下有效。太少,主题保持空白,太多,主题行整合了正文的几行。
  • 你能举几个正文第一行的例子吗-

标签: regex vba outlook


【解决方案1】:

试试这个:

If Len(Trim(objMail.subject)) = 0 Then
     'Take first line of body as subject
     objMail.subject = FirstLineOfText(objMail.GetInspector.WordEditor)
End If

返回第一行文本的函数:

Function FirstLineOfText(doc As Word.Document)
    Dim p As Word.Paragraph, rng
    For Each p In doc.Paragraphs
        'Find the first paragraph with content
        If Len(p.Range.Text) > 2 Then
            'select the start point of the paragraph
            doc.Range(p.Range.Start, p.Range.Start).Select
            'extend the selection to include the whole line
            doc.Application.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            FirstLineOfText = Trim(doc.Application.Selection.Text) '<<EDITED
            Exit Function
        End If
    Next p
End Function

【讨论】:

  • 谢谢,蒂姆。我试过这个,但我得到一个运行时错误,说没有为“FirstLineOfText = Trim(Selection.Text)”行设置对象变量
  • 对不起,我错过了选择的应用程序上下文(包括在上一行中)
  • 效果很好。我无法告诉你你为我和我的同事节省了多少时间。当我告诉他们时,他们会非常高兴。非常非常感谢威廉姆斯先生
【解决方案2】:

试一试:

If TypeOf Item Is MailItem Then
   Set objMail = Item

   If Len(Trim(objMail.Subject)) = 0 Then
       Set objMailDocument = objMail.GetInspector.WordEditor
       Set objMailSelection = objMailDocument.Application.Selection

       objMailDocument.Range(0, 0).Select
       objMailSelection.MoveEnd wdLine

       'Loop until we find some text
       Do While objMailSelection.Text = ""
          objMailSelection.MoveEnd wdLine
       Loop

       'Take first line of body as subject
       objMail.Subject = objMailSelection.Text
   End If
End If

【讨论】:

  • 感谢您的回答。可悲的是,当主题行上方有换行符/空行时,我仍然没有得到任何内容。
  • 我越看代码越不明白为什么它不起作用。这是因为电子邮件中确实有一个主题。它不会以“无主题”的形式出现;它是空白的。因此,激活循环是有意义的。 wdline 的几次迭代确实有效地向下移动了消息。我尝试了其他人发布的涉及重复该行的解决方案。它有效,但问题是它也会开始捕捉相关行下方的文本。真的,我不明白...
  • @Louis 当您说“在相关行下方捕获文本”时,您是什么意思?是否用回车分隔?
  • 主题行也将由相关行之后的行填充,如果我只插入其中的六个:objMailSelection.MoveEnd wdLine
【解决方案3】:

您是否尝试过使用Regular Expression (regex or regexp for short)

https://regex101.com/r/msJ13L/2


"^\w(.*)$"

^ 在行首断言位置

\w 匹配任何单词字符(等于 [a-zA-Z0-9_])

第一捕获组(.*)

.* 匹配任何字符(行终止符除外)

* 量词 - 匹配零次到无限次,尽可能多次,按需回馈(贪婪)

$ 在行尾声明位置 全局模式标志

m 修饰符:多行。导致 ^$ 匹配每行的开始/结束(不仅是字符串的开始/结束)


VBA 示例

Option Explicit
Public Sub Example()
    Dim Matches As Variant        
    Dim Item As MailItem
    Set Item = ActiveExplorer.selection(1)

    Dim RegExp As Object
    Set RegExp = CreateObject("VbScript.RegExp")

    Dim Pattern As String
    Pattern = "^\w(.*)$"
    With RegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = Pattern
         Set Matches = .Execute(Item.Body)
    End With

    If Matches.Count > 0 Then
        Debug.Print Matches(0) ' Print on Immediate Window
    Else
        Debug.Print "Not Found "
    End If

    Set RegExp = Nothing
End Sub

【讨论】:

  • 感谢@0m3r 的回答。我试过你写的,但我无法让它工作。由于我对 VBA 的了解非常有限,我毫不怀疑这是我的错误。
猜你喜欢
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 2015-08-30
  • 2018-10-05
相关资源
最近更新 更多