Диапазон настройки на основе выбора Outlook VBA - PullRequest
1 голос
/ 11 июня 2019

Я хочу создать макрос, который берет ссылочный номер в электронном письме, который я могу выделить и заменить прямой ссылкой на веб-страницу.

Текущий код будет помещать новую гиперссылку в начале электронного письма вместо выбранных областей (в настоящее время wddoc.Range (0, 0)). Я укажу линию, где я не могу понять, каким должен быть диапазон.

Если я использую «Выбор». он говорит, что переменная не определена пользователем.

 Sub AddHyperlink()
 Dim olEmail As Outlook.MailItem
 Dim olInsp As Outlook.Inspector
 Dim wdDoc As Object
 Dim oLink As Object
 Dim oRng As Object
 Dim strLink As String
 Dim strLinkText As String
 Dim OutApp As Object
 Dim OutMail As Object
 Dim strText As String

On Error Resume Next

'Get Outlook if it's running

Set OutApp = GetObject(, "Outlook.Application")



'Outlook wasn't running, so cancel

If Err <> 0 Then

    MsgBox "Outlook is not running so nothing can be selected!"

    GoTo lbl_Exit

End If

On Error GoTo 0



Set OutMail = OutApp.ActiveExplorer.Selection.Item(1)

With OutMail

    Set olInsp = .GetInspector

    Set wdDoc = olInsp.WordEditor

    strText = wdDoc.Application.Selection.Range.Text

End With



strLink = "http://website.com/#" & strText & "" ' the link address

strLinkText = "" & strText & "" ' the link display text



On Error Resume Next

Set olEmail = ActiveInspector.CurrentItem

With olEmail

    .BodyFormat = olFormatHTML

    Set olInsp = .GetInspector

    Set wdDoc = olInsp.WordEditor

    Set oRng = wdDoc.Range(0, 0) '!!!Cannot find something that replaces range with current selection!!!!

    oRng.Collapse 0

    Set oLink = wdDoc.Hyperlinks.Add(Anchor:=oRng, _

                         Address:=strLink, _

                         SubAddress:="", _

                         ScreenTip:="", _

                         TextToDisplay:=strLinkText)

    Set oRng = oLink.Range

    oRng.Collapse 0

    .Display

End With

lbl_Exit:

Exit Sub

End Sub

1 Ответ

0 голосов
/ 11 июня 2019

Outlook vba при работе с ActiveInspector , попробуйте следующее.

Option Explicit
Public Sub Example()
    Dim wdDoc As Word.Document
    Dim rngSel As Word.selection

    If Application.ActiveInspector.EditorType = olEditorWord Then
        Set wdDoc = Application.ActiveInspector.WordEditor ' use WordEditor
        Set rngSel = wdDoc.Windows(1).selection ' Current selection

        wdDoc.Hyperlinks.Add rngSel.Range, _
        Address:="U:\plot.log", TextToDisplay:="Here is the link"
    End If

    Set wdDoc = Nothing
End Sub
...