简体   繁体   中英

Outlook to Excel hyperlink issue

Hoping you can help me understand what i am doing wrong here.

As part of my Outlook macro, i am looking to update a cell in excel with a hyperlink to a document.

'~~> Excel variables
Dim oXLApp As Object, oXLwb As Object, oXLws As Object

'~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")
    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0
'~~> Show Excel
    oXLApp.Visible = True
    '~~> Open the relevant file
    Set oXLwb = oXLApp.Workbooks.Open("V:\Dir\filename.xls")

    '~~> Set the relevant output sheet. Change as applicable
    Set oXLws = oXLwb.Sheets("Outstanding")

       oXLws.Range("R11").Select
       oXLws.Range("R11").Hyperlinks.Add Anchor:=Selection, Address:= _
        "V:\Dir\" & emailsub & ".msg" _
        , TextToDisplay:="Here"

For some reason it just debugs, the code works fine from excel, so i must be missing something, please help!

Cheers, Dom

Since you are latebinding with Excel, Outlook doesn't understand what is Selection

Change these lines

oXLws.Range("R11").Select
oXLws.Range("R11").Hyperlinks.Add Anchor:=Selection, Address:= _
"V:\Dir\" & emailsub & ".msg", TextToDisplay:="Here"

To

oXLws.Range("R11").Hyperlinks.Add Anchor:=oXLws.Range("R11"), Address:= _
"V:\Dir\" & emailsub & ".msg", TextToDisplay:="Here"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM