簡體   English   中英

VB 宏從電子郵件主題或正文中復制選定的文本,將其放入鏈接並在瀏覽器中打開

[英]VB Macro to copy selected text from the email subject or body, put it into a link and open it in a browser

我正在使用:Outlook Office for 365(桌面)

我試圖讓一個 VB 宏在運行時應該復制選定的文本並將其放入預定義的網址並在默認瀏覽器中打開它。

腳步:

  1. 打開或選擇收到的消息
  2. 從電子郵件中選擇一個詞(所選詞可能在電子郵件的主題或正文中)
  3. 運行宏
  4. 默認瀏覽器應打開鏈接: https ://www.dictionary.com/browse/*selected word*

到目前為止,我擁有的代碼如下所示。 但是,這僅在所選單詞位於電子郵件正文中時才會打開鏈接。

Sub OPEN_DICT()

Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Dim vURL1
Dim vURL2
Dim fullVID
Dim fullVURL

If Application.ActiveInspector Is Nothing Then
    If Application.ActiveExplorer.Selection.Count = 1 Then
        If Application.ActiveExplorer.Selection.Item(1).Class = olMail Then
            Set msg = Application.ActiveExplorer.Selection.Item(1)
End If
Else
    'to many items selected
    MsgBox "Please select one item"
End If
Else
    Set insp = Application.ActiveInspector
    If insp.CurrentItem.Class = olMail Then
        Set msg = insp.CurrentItem
    End If
End If

If msg Is Nothing Then
    MsgBox "could not determine an item. Try again!"
Else
    If msg.GetInspector.EditorType = olEditorWord Then
        Set hed = msg.GetInspector.WordEditor
        Set appWord = hed.Application
        Set Rng = appWord.Selection
    With Rng
.Copy
End With

    End If
End If

If IsNumeric(Rng) Then
fullVID = Rng
vURL1 = "https://www.dictionary.com/browse/"
vURL2 = fullVID
fullVURL = vURL1 & vURL2

Else
fullVID = Rng
vURL1 = "https://www.dictionary.com/browse/"
vURL2 = fullVID
fullVURL = vURL1 & vURL2

End If

    Dim build
Set build = CreateObject("Shell.Application")
build.ShellExecute "Chrome.exe", fullVURL, "", "", 1

ExitNewItem:
    Exit Sub

Set appWord = Nothing
Set insp = Nothing
Set Rng = Nothing
Set hed = Nothing
Set msg = Nothing

End Sub

有人可以幫我調整宏,以便電子郵件主題中的選定文本也可以在鏈接中打開嗎? 另外,不是在 Chrome 中打開鏈接,而是應該在默認瀏覽器中打開?

如果問的太多了,我很抱歉。 我對宏非常陌生,並且以某種方式學習,復制粘貼和嘗試。 在這里需要很大的幫助! 謝謝!

Outlook 對象模型不公開任何可讓您從任何控件訪問選定(或任何其他)文本的內容,但郵件正文編輯器(作為Word.Document對象公開)除外。

您最好的選擇是使用低級 Windows API( FindWindow等)或自動化或輔助功能 API 來查找主題編輯器。 例如,請參閱如何獲取當前聚焦窗口的選定文本? 如何將任何應用程序的選定文本放入 Windows 窗體應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM