【问题标题】:Inserting hyperlink in Outlook body using Word VBA使用 Word VBA 在 Outlook 正文中插入超链接
【发布时间】:2018-06-06 01:51:43
【问题描述】:

如何将超链接添加到电子邮件的最后部分并结合文本?

这一定是我段落的最后一句话

“如果您有任何问题/澄清,请通过 Service_Management@xyz.com 与服务管理部门联系”

“Service_Management@xyz.com”这个词必须有一个超链接。

我尝试从这里编辑代码:

wd.Hyperlinks.Add wd.Range(wd.Paragraphs.Count), _
  "mailto:Service_Management@xyz.com" & "Service_Management@xyz.com"

问题是电子邮件正文的底部变成了一个链接。并且电子邮件正文中没有文本显示。如何正确声明链接,以免影响范围的其他部分?

 Sub AUTOMAIL()

    Dim ol As Object 'Outlook.Application
    Dim olEmail As Object 'Outlook.MailItem
    Dim olInsp As Object 'Outlook.Inspector
    Dim wd As Object 'Word.Document
    Dim rCol As Collection, r As Range, i As Integer
    Dim Table1 As Collection

    Dim ETo As String
    Dim CTo As String

    ETo = Join(Application.Transpose(Worksheets("Data Entry").Range("AD5:AD100").Value), ";")
    CTo = Join(Application.Transpose(Worksheets("Data Entry").Range("AI5:AI15").Value), ";")

     '/* if outlook is running use GO, create otherwise */
    Set ol = GetObject(Class:="Outlook.Application")
    Set olEmail = ol.CreateItem(0) 'olMailItem

    Set Table1 = New Collection

    With Table1
        .Add Sheet14.Range("A1:O20")
    End With

    Set rCol = New Collection
    With rCol   
        .Add Sheet11.Range("a1:i1", "a6:i20")
        .Add Sheet10.Range("a1:i1", "A6:I20")
        .Add Sheet9.Range("A1:J18")
    End With

    With olEmail
        .To = ETo
        .CC = CTo
        .Subject = "Step+ Volume Tracker, Data Entry/Workflow Ageing Report and Rejection Report | " & Format(Date, "MMMM dd, yyyy") & " | 9:15AM"

        '/* bonus basic html */
        .HTMLBody = "<html><body style=""font-family:calibri"">" & _
                    "<p><b>Dear All,</b><br><br> Please see below summary of invoices and links to the <b>Volume Tracker</b> and <b>Ageing Report</b> (Data Entry and Workflow)." & _
                    "</p></body></html>"

           Set olInsp = .GetInspector
           If olInsp.EditorType = 4 Then 'olEditorWord
               Set wd = olInsp.WordEditor
               For i = 1 To Table1.Count '/* iterate all ranges */
                   Set r = Table1.Item(i): r.Copy
                   wd.Range.insertparagraphafter
                   wd.Paragraphs(wd.Paragraphs.Count).Range.PasteAndFormat 16
                   '16 - wdFormatOriginalFormatting
            Next
        End If
        wd.Paragraphs(wd.Paragraphs.Count).Range.Text = Chr(11) & "Please click on this link to view the details:"

        Set olInsp = .GetInspector
        If olInsp.EditorType = 4 Then 'olEditorWord
            Set wd = olInsp.WordEditor
            For i = 1 To rCol.Count '/* iterate all ranges */
                Set r = rCol.Item(i): r.Copy
                wd.Range.insertparagraphafter
                wd.Paragraphs(wd.Paragraphs.Count).Range.PasteAndFormat 16
                '16 - wdFormatOriginalFormatting
            Next
        End If

        wd.Paragraphs(wd.Paragraphs.Count).Range.Text = Chr(11) & "Please click on this link to view the details:" & vbCrLf & "Those who are encountering problems accessing the Sharepoint site, please refer to attachment for Data Entry and Workflow Report. " & Chr(11) & "Please note though that the file has been truncated, complete details of the report are available in the links indicated above." & Chr(11) & Chr(11) & "Should you have questions/clarifications, kindly reach out to Service Management at"
        wd.Range(wd.Paragraphs(wd.Paragraphs.Count).Range.Characters(98).Start, _
        wd.Paragraphs(wd.Paragraphs.Count).Range.Characters(128).End).Font.Bold = True

        wd.Range.Hyperlinks.Add Anchor:=wd.Range, _
        Address:="mailto:Service_Management@xyz.com"
        wd.Range.Font.Size = 10
        .Display

    End With

End Sub

【问题讨论】:

  • 在这个问题上我真的需要帮助,请帮忙。
  • 我通常用普通的旧html添加它:"Contact the &lt;a href=" &amp; Chr(34) &amp; "mailto:whatever@whatever.com" &amp; Chr(34) &amp; "&gt;Support Team&lt;/a&gt;."
  • 我已经尝试帮助您解决同样的问题,您现在显然已将其删除。如果您无法回答人们在试图提供帮助时提出的问题,那么您可能永远得不到满意的答案。
  • @KostasK。我需要将它插入电子邮件正文的底部,我不能使用 html,因为它已在我的电子邮件开头使用过,顺便感谢您的见解。

标签: excel vba outlook hyperlink ms-word


【解决方案1】:

将所选内容移至消息末尾,然后插入超链接。

'Reference to Word Object Library required
 objSel.EndKey Unit:=wdStory

' Reference to Word Object Library not required
objSel.EndKey Unit:=6

这是一个比您问题中的代码少嘈杂的示例。

Sub AUTOMAIL()

    Dim ol As Object 'Outlook.Application
    Dim olEmail As Object 'Outlook.MailItem
    Dim olInsp As Object 'Outlook.Inspector
    Dim wd As Object 'Word.Document

     '/* if outlook is running use GO, create otherwise */
    Set ol = GetObject(Class:="Outlook.Application")
    Set olEmail = ol.CreateItem(0) 'olMailItem

    With olEmail

        '/* bonus basic html */
        .HTMLBody = "<html><body style=""font-family:calibri"">" & _
                    "<p><b>Dear All,</b><br><br> Please see below summary of invoices and links to the <b>Volume Tracker</b> and <b>Ageing Report</b> (Data Entry and Workflow)." & _
                    "</p></body></html>"

        Set olInsp = .GetInspector
        If olInsp.EditorType = 4 Then 'olEditorWord
            Set wd = olInsp.WordEditor
            wd.Range.InsertParagraphAfter
        End If

        wd.Paragraphs(wd.Paragraphs.Count).Range.Text = Chr(11) & "Please click on this link to view the details:" & vbCrLf & "Those who are encountering problems accessing the Sharepoint site, please refer to attachment for Data Entry and Workflow Report. " & Chr(11) & "Please note though that the file has been truncated, complete details of the report are available in the links indicated above." & Chr(11) & Chr(11) & "Should you have questions/clarifications, kindly reach out to Service Management at"

        wd.Range.InsertParagraphAfter

        Dim objSel As Object
        Set objSel = wd.Windows(1).Selection

        'Reference to Word Object Library required
        'objSel.EndKey Unit:=wdStory

        ' Reference to Word Object Library not required
        objSel.EndKey Unit:=6

        wd.Range.Hyperlinks.Add Anchor:=objSel.Range, _
          Address:="mailto:Service_Management@xyz.com"

        .Display

    End With

End Sub

【讨论】:

  • 如何在代码底部添加签名?
  • 搜索网站,如有必要,提出新问题。
猜你喜欢
  • 2016-06-23
  • 2021-08-07
  • 1970-01-01
  • 2016-09-22
  • 2011-09-15
  • 2013-10-02
  • 2021-06-27
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多