[英]VB Macro to copy selected text from the email subject or body, put it into a link and open it in a browser
I am Using: Outlook office for 365 (desktop)我正在使用:Outlook Office for 365(桌面)
I am trying to have a VB macro that on running should copy the selected text and put it into a predefined web address and open it in the default browser.我试图让一个 VB 宏在运行时应该复制选定的文本并将其放入预定义的网址并在默认浏览器中打开它。
Steps:脚步:
The code so far I have is the one shown below.到目前为止,我拥有的代码如下所示。 However, this only opens the link when the selected word is in the body of the email.但是,这仅在所选单词位于电子邮件正文中时才会打开链接。
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
Can someone please help me in adjusting the macro so that the selected text from the Subject of the email can also be opened in the link?有人可以帮我调整宏,以便电子邮件主题中的选定文本也可以在链接中打开吗? Also, instead of opening the link in Chrome, it should open in the default browser?另外,不是在 Chrome 中打开链接,而是应该在默认浏览器中打开?
I am sorry if this is too much to ask.如果问的太多了,我很抱歉。 I am very new to macros and somehow learning, copy-pasting and trying.我对宏非常陌生,并且以某种方式学习,复制粘贴和尝试。 Need a big help here!在这里需要很大的帮助! THANKS!谢谢!
Outlook Object Model does not expose anything that would let you access selected (or any other) text from any of its controls except for the message body editor (which is exposed as Word.Document
object). Outlook 对象模型不公开任何可让您从任何控件访问选定(或任何其他)文本的内容,但邮件正文编辑器(作为Word.Document
对象公开)除外。
Your best bet would be finding the Subject editor using low-level Windows API ( FindWindow
etc.) or Automation or Accessibility API to do that.您最好的选择是使用低级 Windows API( FindWindow
等)或自动化或辅助功能 API 来查找主题编辑器。 See, for example, How to get selected text of currently focused window?例如,请参阅如何获取当前聚焦窗口的选定文本? or How to get selected text of any application into a windows form application或如何将任何应用程序的选定文本放入 Windows 窗体应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.