Outlook VBA

Instead of attaching documents to an email, I prefer to shows links.

I use the Windows explorer ribbon button "Copy path"

This can be pasted in an Outlook email, but requires trimming the quotes and calling "Insert" "Link".

I would like to automate this, presumably via VBA.  Would anyone have a thought on this?
V MAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub PasteLinkFromClipboard()

    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim DataObj As Object
    
    ' add reference to Microsoft Forms 2.0 Object Library
    Set DataObj = New MSForms.DataObject
    DataObj.GetFromClipboard
    strLink = DataObj.GetText(1)

    Set objItem = Application.ActiveInspector.CurrentItem
    Set objInsp = objItem.GetInspector
    Set objDoc = objInsp.WordEditor
    Set objWord = objDoc.Application
    Set objSel = objWord.Selection
    
    objDoc.Hyperlinks.Add objSel.Range, strLink, "", "", strLink, ""

    Set DataObj = Nothing
    Set objItem = Nothing
    Set objInsp = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing
    Set objSel = Nothing

 End Sub

Open in new window

Regards
V MAuthor Commented:
Thank you!  that is terrific. The one glitch is that the strLink contains quotes marks, and those need to be removed in order for the link to work.  Can you suggest how to do that?  thank you.
Rgonzo1971Commented:
then try
Sub PasteLinkFromClipboard()

    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim DataObj As Object
    
    ' add reference to Microsoft Forms 2.0 Object Library
    Set DataObj = New MSForms.DataObject
    DataObj.GetFromClipboard
    strLink = DataObj.GetText(1)
    strLink = Mid(strLink, 2, Len(strLink)-2)

    Set objItem = Application.ActiveInspector.CurrentItem
    Set objInsp = objItem.GetInspector
    Set objDoc = objInsp.WordEditor
    Set objWord = objDoc.Application
    Set objSel = objWord.Selection
    
    objDoc.Hyperlinks.Add objSel.Range, strLink, "", "", strLink, ""

    Set DataObj = Nothing
    Set objItem = Nothing
    Set objInsp = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing
    Set objSel = Nothing

 End Sub

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

V MAuthor Commented:
You're the best!  that's what it needed. Thank you.
V MAuthor Commented:
at the risk of looking greedy, I notice that Windows Explorer allows me to copy multiple paths when multiple files are selected.

When i use the above code, the result produces very nice multiple links, but the quotes are removed only from the beginning and end of the string.  Is there a way to remove all quote marks with something like:

    replace(strLink,""","",) as string
Rgonzo1971Commented:
like this
strLink = replace(strLink, """", "")
V MAuthor Commented:
a loop perhaps would be needed
Rgonzo1971Commented:
or
strLink = replace(strLink, Chr(34), "")

Open in new window

V MAuthor Commented:
the replace command did get rid of the quotes nicely, however, the result shows an icon of a small question mark inside a box at the beginning of the second line and subsequent lines (depending on how many file paths were copied).

I think the solution to this (copying multiple file paths) will require a loop so that each file path is made into a separate link, as opposed to all file paths being made into one big link.

I think I was a bit greedy,

You've been a great help, and made my day.  thank you!
Rgonzo1971Commented:
then try
Sub PasteLinkFromClipboard()

    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim DataObj As Object
    
    ' add reference to Microsoft Forms 2.0 Object Library
    Set DataObj = New MSForms.DataObject
    DataObj.GetFromClipboard
    strLink = DataObj.GetText(1)
    strLink = Replace(strLink, Chr(34), "")
    arrLinks = Split(strLink, vbCrLf)

    Set objItem = Application.ActiveInspector.CurrentItem
    Set objInsp = objItem.GetInspector
    Set objDoc = objInsp.WordEditor
    Set objWord = objDoc.Application
    Set objSel = objWord.Selection
    
    For Each lnk In arrLinks
        objDoc.Hyperlinks.Add objSel.Range, lnk, "", "", lnk, ""
        objSel.Collapse 1 '  wd CollapseEnd
        objSel.InsertParagraph
        objSel.Collapse 0
    Next

    Set DataObj = Nothing
    Set objItem = Nothing
    Set objInsp = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing
    Set objSel = Nothing

 End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
V MAuthor Commented:
Wow!  that is perfect!  Thank you!
Rgonzo1971Commented:
Solution according to author's comment
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.